Skip to content

Commit

Permalink
✨ FEAT. webhook 응답 복호화 기능 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
junhaa committed May 7, 2024
1 parent 9804987 commit eae75b0
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ dependencies {
implementation 'com.squareup.okhttp3:okhttp:3.10.0'
implementation 'org.json:json:20200518'

// webhook
implementation 'javax.xml.bind:jaxb-api:2.3.1'

// Spring Security OAUTH 2.1
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.security:spring-security-oauth2-client'
Expand Down
95 changes: 95 additions & 0 deletions src/main/java/fairytale/tbd/domain/faceSwap/util/CryptoUtils.java
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;
}
}

0 comments on commit eae75b0

Please sign in to comment.