parent
7dc81e6873
commit
8521a9b42a
@ -1,53 +0,0 @@
|
||||
package UnitTest;
|
||||
|
||||
import java.util.Base64;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class KeyValidator {
|
||||
|
||||
public static boolean validateKey(String key, String sharedSecret) {
|
||||
try {
|
||||
String[] parts = key.split(":");
|
||||
if (parts.length != 3) {
|
||||
return false; // Invalid key format
|
||||
}
|
||||
|
||||
String secret = parts[0];
|
||||
String timestampStr = parts[1];
|
||||
String expirationStr = parts[2];
|
||||
|
||||
long timestamp = Long.parseLong(timestampStr);
|
||||
long expiration = Long.parseLong(expirationStr);
|
||||
|
||||
// Check if the key has expired
|
||||
if (System.currentTimeMillis() > expiration * 1000) {
|
||||
return false; // Key has expired
|
||||
}
|
||||
|
||||
// Recreate the key for verification
|
||||
String keyData = String.join(":", secret, timestampStr, expirationStr);
|
||||
String generatedKey = generateKey(sharedSecret, keyData);
|
||||
|
||||
return key.equals(generatedKey); // Validate the key
|
||||
} catch (Exception e) {
|
||||
return false; // Error in processing, consider the key invalid
|
||||
}
|
||||
}
|
||||
|
||||
private static String generateKey(String secret, String keyData) throws NoSuchAlgorithmException {
|
||||
MessageDigest md = MessageDigest.getInstance("SHA-256");
|
||||
md.update((keyData + secret).getBytes());
|
||||
byte[] digest = md.digest();
|
||||
return Base64.getUrlEncoder().encodeToString(digest);
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
String sharedSecret = "DsideaL4r5t6y7u@123"; // This should match the one used in Python
|
||||
String key = "0ede740c2c81ea362dcdeee568716eb06706d96dda55be6109bb8cf2c83344c1"; // Example key to validate
|
||||
|
||||
boolean isValid = validateKey(key, sharedSecret);
|
||||
System.out.println("Is the key valid? " + isValid);
|
||||
}
|
||||
}
|
Loading…
Reference in new issue