-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
98 additions
and
0 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
95 changes: 95 additions & 0 deletions
95
src/main/java/fairytale/tbd/domain/faceSwap/util/CryptoUtils.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,95 @@ | ||
package fairytale.tbd.domain.faceSwap.util; | ||
|
||
import java.security.MessageDigest; | ||
import javax.crypto.Cipher; | ||
import javax.crypto.spec.IvParameterSpec; | ||
import javax.crypto.spec.SecretKeySpec; | ||
import java.util.Arrays; | ||
import java.nio.charset.StandardCharsets; | ||
import javax.xml.bind.DatatypeConverter; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class CryptoUtils { | ||
|
||
@Value("${face.akool.apikey}") | ||
private String clientSecret; | ||
|
||
@Value("${face.akool.clientId}") | ||
private String clientId; | ||
|
||
|
||
// Generate signature | ||
public static String generateMsgSignature(String clientId, String timestamp, String nonce, String msgEncrypt) { | ||
String[] arr = {clientId, timestamp, nonce, msgEncrypt}; | ||
Arrays.sort(arr); | ||
String sortedStr = String.join("", arr); | ||
return sha1(sortedStr); | ||
} | ||
|
||
// SHA-1 hash function | ||
private static String sha1(String input) { | ||
try { | ||
MessageDigest md = MessageDigest.getInstance("SHA-1"); | ||
byte[] hashBytes = md.digest(input.getBytes(StandardCharsets.UTF_8)); | ||
return DatatypeConverter.printHexBinary(hashBytes).toLowerCase(); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
return null; | ||
} | ||
} | ||
|
||
// Decryption algorithm | ||
public static String generateAesDecrypt(String dataEncrypt, String clientId, String clientSecret) { | ||
try { | ||
byte[] keyBytes = clientSecret.getBytes(StandardCharsets.UTF_8); | ||
byte[] ivBytes = clientId.getBytes(StandardCharsets.UTF_8); | ||
|
||
SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES"); | ||
IvParameterSpec ivSpec = new IvParameterSpec(ivBytes); | ||
|
||
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); | ||
cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec); | ||
|
||
byte[] encryptedBytes = DatatypeConverter.parseHexBinary(dataEncrypt); | ||
byte[] decryptedBytes = cipher.doFinal(encryptedBytes); | ||
|
||
return new String(decryptedBytes, StandardCharsets.UTF_8); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
return null; | ||
} | ||
} | ||
|
||
// Encryption algorithm | ||
public static String generateAesEncrypt(String data, String clientId, String clientSecret) { | ||
try { | ||
byte[] keyBytes = clientSecret.getBytes(StandardCharsets.UTF_8); | ||
byte[] ivBytes = clientId.getBytes(StandardCharsets.UTF_8); | ||
|
||
SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES"); | ||
IvParameterSpec ivSpec = new IvParameterSpec(ivBytes); | ||
|
||
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); | ||
cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec); | ||
|
||
byte[] encryptedBytes = cipher.doFinal(data.getBytes(StandardCharsets.UTF_8)); | ||
return DatatypeConverter.printHexBinary(encryptedBytes).toLowerCase(); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
return null; | ||
} | ||
} | ||
|
||
// Example usage | ||
public String getURL(String timestamp, String nonce, String msgEncrypt, String signature){ | ||
String newSignature = generateMsgSignature(clientId, timestamp, nonce, msgEncrypt); | ||
if (signature.equals(newSignature)) { | ||
String result = generateAesDecrypt(msgEncrypt, clientId, clientSecret); | ||
return result; | ||
} | ||
return null; | ||
} | ||
} |