mirror of
https://gitcode.com/gh_mirrors/on/onvif.git
synced 2025-12-30 05:22:27 +00:00
feat: Add test for nonce
This commit is contained in:
40
onvif-java/src/test/java/org/onvif/client/AuthTest.java
Normal file
40
onvif-java/src/test/java/org/onvif/client/AuthTest.java
Normal file
@@ -0,0 +1,40 @@
|
||||
package org.onvif.client;
|
||||
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.Base64;
|
||||
|
||||
public class AuthTest {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
||||
String date = "2024-03-06T06:04:30Z";
|
||||
String nonce = "gHV4Y/cV8KE7OSY7+qGv0g==";
|
||||
String password = "abcd1234";
|
||||
|
||||
try {
|
||||
// 1. Base64 decode the nonce
|
||||
byte[] decodedNonce = Base64.getDecoder().decode(nonce);
|
||||
|
||||
// 2. Concatenate decodedNonce, date, and password
|
||||
byte[] dateBytes = date.getBytes();
|
||||
byte[] passwordBytes = password.getBytes();
|
||||
byte[] toBeHashed = new byte[decodedNonce.length + dateBytes.length + passwordBytes.length];
|
||||
|
||||
System.arraycopy(decodedNonce, 0, toBeHashed, 0, decodedNonce.length);
|
||||
System.arraycopy(dateBytes, 0, toBeHashed, decodedNonce.length, dateBytes.length);
|
||||
System.arraycopy(passwordBytes, 0, toBeHashed, decodedNonce.length + dateBytes.length, passwordBytes.length);
|
||||
|
||||
// 3. SHA-1 hash the concatenated bytes
|
||||
MessageDigest digest = MessageDigest.getInstance("SHA-1");
|
||||
byte[] sha1Hash = digest.digest(toBeHashed);
|
||||
|
||||
// 4. Base64 encode the SHA-1 hash
|
||||
String digestBase64 = Base64.getEncoder().encodeToString(sha1Hash);
|
||||
|
||||
System.out.println("Digest: " + digestBase64);
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
28
onvif-java/src/test/java/org/onvif/client/Base64Test.java
Normal file
28
onvif-java/src/test/java/org/onvif/client/Base64Test.java
Normal file
@@ -0,0 +1,28 @@
|
||||
package org.onvif.client;
|
||||
|
||||
import java.util.Base64;
|
||||
|
||||
public class Base64Test {
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
String base64String = "4AUTXXJ214OBnsdeYBXatQ==";
|
||||
|
||||
// decode Base64
|
||||
byte[] decodedBytes = Base64.getDecoder().decode(base64String);
|
||||
|
||||
// turn byte[] to hex
|
||||
StringBuilder hexStringBuilder = new StringBuilder();
|
||||
for (byte b : decodedBytes) {
|
||||
String hex = Integer.toHexString(0xff & b);
|
||||
if (hex.length() == 1) {
|
||||
hexStringBuilder.append('0');
|
||||
}
|
||||
hexStringBuilder.append(hex);
|
||||
}
|
||||
String hexString = hexStringBuilder.toString();
|
||||
|
||||
System.out.println("hex:" + hexString);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user