-
Notifications
You must be signed in to change notification settings - Fork 0
Add key generation, service and controller #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
MiniKms/src/main/java/ftn/security/minikms/controller/KeyManagementController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| package ftn.security.minikms.controller; | ||
|
|
||
| import ftn.security.minikms.dto.KeyDTO; | ||
| import ftn.security.minikms.service.KeyService; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.*; | ||
|
|
||
| import java.security.NoSuchAlgorithmException; | ||
|
|
||
| @RestController | ||
| @RequestMapping(value = "/api/v1/keys") | ||
| public class KeyManagementController { | ||
| @Autowired | ||
| private KeyService keyService; | ||
|
|
||
| @PostMapping("/create") | ||
| public ResponseEntity<KeyDTO> createKey(@RequestBody KeyDTO dto) throws NoSuchAlgorithmException { | ||
| String id = keyService.createKey(dto.getKeyType()); | ||
| dto.setId(id); | ||
| return new ResponseEntity<>(dto, HttpStatus.CREATED); | ||
| } | ||
| @PostMapping("/rotate") | ||
| public ResponseEntity<KeyDTO> rotateKey(@RequestBody KeyDTO dto){ | ||
| keyService.rotateKey(dto.getKeyType(), dto.getId()); | ||
| return new ResponseEntity<>(dto, HttpStatus.CREATED); | ||
| } | ||
| @PutMapping("/delete/{id}") | ||
| public ResponseEntity<String> deleteKey(@PathVariable String id){ | ||
| keyService.deleteKey(id); | ||
| return new ResponseEntity<>(id, HttpStatus.OK); | ||
| } | ||
|
|
||
| } | ||
16 changes: 16 additions & 0 deletions
16
MiniKms/src/main/java/ftn/security/minikms/dto/KeyDTO.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package ftn.security.minikms.dto; | ||
|
|
||
| import lombok.AllArgsConstructor; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
| import lombok.Setter; | ||
|
|
||
| @Getter | ||
| @Setter | ||
| @NoArgsConstructor | ||
| @AllArgsConstructor | ||
| public class KeyDTO { | ||
| private String id; | ||
| private String alias; | ||
| private String keyType; | ||
| } |
21 changes: 21 additions & 0 deletions
21
MiniKms/src/main/java/ftn/security/minikms/service/AES.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package ftn.security.minikms.service; | ||
|
|
||
| import javax.crypto.KeyGenerator; | ||
| import javax.crypto.SecretKey; | ||
| import java.security.NoSuchAlgorithmException; | ||
|
|
||
| public class AES { | ||
| public static String createKey() throws NoSuchAlgorithmException { | ||
| SecretKey key = generateKey(); | ||
| //save key | ||
| return "keyId"; | ||
| } | ||
| private static SecretKey generateKey() throws NoSuchAlgorithmException { | ||
| KeyGenerator keyGenerator = KeyGenerator.getInstance("AES"); | ||
| keyGenerator.init(256); | ||
| return keyGenerator.generateKey(); | ||
| } | ||
| public static void rotateKey(String Id){ | ||
| //rotate | ||
| } | ||
| } |
22 changes: 22 additions & 0 deletions
22
MiniKms/src/main/java/ftn/security/minikms/service/HMAC.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| package ftn.security.minikms.service; | ||
|
|
||
| import javax.crypto.KeyGenerator; | ||
| import javax.crypto.SecretKey; | ||
| import java.security.NoSuchAlgorithmException; | ||
| import java.security.SecureRandom; | ||
|
|
||
| public class HMAC { | ||
| public static String createKey() throws NoSuchAlgorithmException { | ||
| SecretKey key = generateKey(); | ||
| //save key | ||
| return "keyId"; | ||
| } | ||
| private static SecretKey generateKey() throws NoSuchAlgorithmException { | ||
| KeyGenerator keyGenerator = KeyGenerator.getInstance("HmacSHA512"); | ||
| keyGenerator.init(256, SecureRandom.getInstanceStrong()); | ||
| return keyGenerator.generateKey(); | ||
| } | ||
| public static void rotateKey(String Id){ | ||
| //rotate | ||
| } | ||
| } |
29 changes: 29 additions & 0 deletions
29
MiniKms/src/main/java/ftn/security/minikms/service/KeyService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| package ftn.security.minikms.service; | ||
|
|
||
| import org.springframework.stereotype.Service; | ||
|
|
||
| import java.security.InvalidParameterException; | ||
| import java.security.NoSuchAlgorithmException; | ||
|
|
||
| @Service | ||
| public class KeyService { | ||
| public String createKey(String keyType) throws NoSuchAlgorithmException, InvalidParameterException { | ||
| return switch (keyType) { | ||
| case "symmetric" -> AES.createKey(); | ||
| case "asymmetric" -> RSA.createKey(); | ||
| case "hmac" -> HMAC.createKey(); | ||
| default -> throw new InvalidParameterException(); | ||
| }; | ||
| } | ||
| public void deleteKey(String Id){ | ||
| //delete from database | ||
| } | ||
| public void rotateKey(String keyType, String keyId) throws InvalidParameterException { | ||
| switch(keyType){ | ||
| case "symmetric" -> AES.rotateKey(keyId); | ||
| case "asymmetric" -> RSA.rotateKey(keyId); | ||
| case "hmac" -> HMAC.rotateKey(keyId); | ||
| default -> throw new InvalidParameterException(); | ||
| } | ||
| } | ||
| } |
21 changes: 21 additions & 0 deletions
21
MiniKms/src/main/java/ftn/security/minikms/service/RSA.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package ftn.security.minikms.service; | ||
|
|
||
| import java.security.KeyPair; | ||
| import java.security.KeyPairGenerator; | ||
| import java.security.NoSuchAlgorithmException; | ||
|
|
||
| public class RSA { | ||
| public static String createKey() throws NoSuchAlgorithmException { | ||
| KeyPair key = generateKey(); | ||
| //save key | ||
| return "keyId"; | ||
| } | ||
| private static KeyPair generateKey() throws NoSuchAlgorithmException { | ||
| KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA"); | ||
| generator.initialize(2048); | ||
| return generator.generateKeyPair(); | ||
| } | ||
| public static void rotateKey(String Id){ | ||
| //rotate | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.