-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improved handling of sensitive data (symmetric encryption of Garmin U…
…serAccesstokenSecret and Polar UserAccessToken)
- Loading branch information
1 parent
b688b26
commit 46bc3ee
Showing
6 changed files
with
97 additions
and
8 deletions.
There are no files selected for viewing
This file contains 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
52 changes: 52 additions & 0 deletions
52
src/main/java/com/cubetrek/database/AESEncryptionService.java
This file contains 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,52 @@ | ||
package com.cubetrek.database; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Service; | ||
|
||
import javax.crypto.Cipher; | ||
import javax.crypto.SecretKey; | ||
import javax.crypto.spec.SecretKeySpec; | ||
import java.security.MessageDigest; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.util.Arrays; | ||
import java.util.Base64; | ||
|
||
@Service | ||
public class AESEncryptionService { | ||
|
||
private static SecretKey secretKey; | ||
|
||
public AESEncryptionService(@Value("${thirdparty.clientkey.key}") String encryptionSecret) { | ||
secretKey = generateSecretKeyFromString(encryptionSecret); | ||
} | ||
|
||
|
||
//Generate a SecretKey from a String by hashing | ||
public static SecretKey generateSecretKeyFromString(String password) { | ||
MessageDigest sha = null; | ||
try { | ||
sha = MessageDigest.getInstance("SHA-256"); | ||
} catch (NoSuchAlgorithmException e) { | ||
throw new RuntimeException(e); | ||
} | ||
byte[] key = sha.digest(password.getBytes()); | ||
key = Arrays.copyOf(key, 32); | ||
|
||
return new SecretKeySpec(key, "AES"); | ||
} | ||
|
||
public String encrypt(String data) throws Exception { | ||
Cipher cipher = Cipher.getInstance("AES"); | ||
cipher.init(Cipher.ENCRYPT_MODE, secretKey); | ||
byte[] encryptedBytes = cipher.doFinal(data.getBytes()); | ||
return Base64.getEncoder().encodeToString(encryptedBytes); | ||
} | ||
|
||
public String decrypt(String encryptedData) throws Exception { | ||
Cipher cipher = Cipher.getInstance("AES"); | ||
cipher.init(Cipher.DECRYPT_MODE, secretKey); | ||
byte[] decodedBytes = Base64.getDecoder().decode(encryptedData); | ||
byte[] decryptedBytes = cipher.doFinal(decodedBytes); | ||
return new String(decryptedBytes); | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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