From dc0a1befcbc557e5a5e5108990e22bc2bbf5f709 Mon Sep 17 00:00:00 2001 From: tornado <972558143@qq.com> Date: Thu, 7 Mar 2024 13:44:05 +0800 Subject: [PATCH] feat: Add test for nonce --- .../test/java/org/onvif/client/AuthTest.java | 40 +++++++++++++++++++ .../java/org/onvif/client/Base64Test.java | 28 +++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 onvif-java/src/test/java/org/onvif/client/AuthTest.java create mode 100644 onvif-java/src/test/java/org/onvif/client/Base64Test.java diff --git a/onvif-java/src/test/java/org/onvif/client/AuthTest.java b/onvif-java/src/test/java/org/onvif/client/AuthTest.java new file mode 100644 index 0000000..c8c5bfa --- /dev/null +++ b/onvif-java/src/test/java/org/onvif/client/AuthTest.java @@ -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(); + } + } +} diff --git a/onvif-java/src/test/java/org/onvif/client/Base64Test.java b/onvif-java/src/test/java/org/onvif/client/Base64Test.java new file mode 100644 index 0000000..51a9a95 --- /dev/null +++ b/onvif-java/src/test/java/org/onvif/client/Base64Test.java @@ -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); + } + +}