You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by ji...@apache.org on 2023/02/16 23:43:14 UTC

[shardingsphere] branch master updated: Refactor OpenGaussAuthenticationSCRAMSha256Packet (#24205)

This is an automated email from the ASF dual-hosted git repository.

jianglongtao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new a61ad67913e Refactor OpenGaussAuthenticationSCRAMSha256Packet (#24205)
a61ad67913e is described below

commit a61ad67913e2ccd4a13ed5880df3cbcd54df66de
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Fri Feb 17 07:42:57 2023 +0800

    Refactor OpenGaussAuthenticationSCRAMSha256Packet (#24205)
    
    * Remove OpenGaussAuthenticationHandler
    
    * Remove OpenGaussAuthenticationHandler
    
    * Refactor OpenGaussAuthenticationSCRAMSha256Packet
---
 .../OpenGaussAuthenticationSCRAMSha256Packet.java  | 47 ++++++------
 ...enGaussAuthenticationSCRAMSha256PacketTest.java |  6 +-
 .../OpenGaussAuthenticationEngine.java             |  2 +-
 .../OpenGaussSCRAMSha256PasswordAuthenticator.java |  5 +-
 .../OpenGaussAuthenticationEngineTest.java         | 83 +++++++++++++++-------
 .../fixture/OpenGaussAuthenticationAlgorithm.java  | 34 ++++-----
 6 files changed, 100 insertions(+), 77 deletions(-)

diff --git a/db-protocol/opengauss/src/main/java/org/apache/shardingsphere/db/protocol/opengauss/packet/authentication/OpenGaussAuthenticationSCRAMSha256Packet.java b/db-protocol/opengauss/src/main/java/org/apache/shardingsphere/db/protocol/opengauss/packet/authentication/OpenGaussAuthenticationSCRAMSha256Packet.java
index 3dc1c6b8385..14238544e94 100644
--- a/db-protocol/opengauss/src/main/java/org/apache/shardingsphere/db/protocol/opengauss/packet/authentication/OpenGaussAuthenticationSCRAMSha256Packet.java
+++ b/db-protocol/opengauss/src/main/java/org/apache/shardingsphere/db/protocol/opengauss/packet/authentication/OpenGaussAuthenticationSCRAMSha256Packet.java
@@ -49,47 +49,40 @@ public final class OpenGaussAuthenticationSCRAMSha256Packet implements PostgreSQ
     
     private static final int PASSWORD_STORED_METHOD_SHA256 = 2;
     
-    private final byte[] random64Code;
-    
-    private final byte[] token;
-    
     private final int version;
     
-    private final byte[] serverSignature;
-    
     private final int serverIteration;
     
-    public OpenGaussAuthenticationSCRAMSha256Packet(final String password, final OpenGaussAuthenticationHexData authHexData, final int version, final int serverIteration) {
-        random64Code = authHexData.getSalt().getBytes();
-        token = authHexData.getNonce().getBytes();
+    private final OpenGaussAuthenticationHexData authHexData;
+    
+    private final String serverSignature;
+    
+    public OpenGaussAuthenticationSCRAMSha256Packet(final int version, final int serverIteration, final OpenGaussAuthenticationHexData authHexData, final String password) {
         this.version = version;
-        serverSignature = (version >= OpenGaussProtocolVersion.PROTOCOL_350.getVersion() ? "" : calculateServerSignature(password, authHexData, serverIteration)).getBytes();
         this.serverIteration = serverIteration;
+        this.authHexData = authHexData;
+        serverSignature = version >= OpenGaussProtocolVersion.PROTOCOL_350.getVersion() ? "" : generateServerSignature(password);
     }
     
-    private static String calculateServerSignature(final String password, final OpenGaussAuthenticationHexData authHexData, final int serverIteration) {
-        byte[] k = generateKFromPBKDF2(password, authHexData.getSalt(), serverIteration);
+    private String generateServerSignature(final String password) {
+        byte[] k = generateKFromPBKDF2(password);
         byte[] serverKey = getKeyFromHmac(k, SERVER_KEY.getBytes(StandardCharsets.UTF_8));
-        byte[] result = getKeyFromHmac(serverKey, hexStringToBytes(authHexData.getNonce()));
+        byte[] result = getKeyFromHmac(serverKey, toHexBytes(authHexData.getNonce()));
         return bytesToHexString(result);
     }
     
     @SneakyThrows({NoSuchAlgorithmException.class, InvalidKeySpecException.class})
-    private static byte[] generateKFromPBKDF2(final String password, final String saltString, final int serverIteration) {
+    private byte[] generateKFromPBKDF2(final String password) {
         char[] chars = password.toCharArray();
-        byte[] salt = hexStringToBytes(saltString);
+        byte[] salt = toHexBytes(authHexData.getSalt());
         PBEKeySpec spec = new PBEKeySpec(chars, salt, serverIteration, 32 * 8);
         SecretKeyFactory skf = SecretKeyFactory.getInstance(PBKDF2_WITH_HMAC_SHA1_ALGORITHM);
         return skf.generateSecret(spec).getEncoded();
     }
     
-    private static byte[] hexStringToBytes(final String rawHexString) {
-        if (null == rawHexString || rawHexString.isEmpty()) {
-            return new byte[0];
-        }
-        String hexString = rawHexString.toUpperCase(Locale.ENGLISH);
+    private byte[] toHexBytes(final String hexString) {
         int length = hexString.length() / 2;
-        char[] hexChars = hexString.toCharArray();
+        char[] hexChars = hexString.toUpperCase(Locale.ENGLISH).toCharArray();
         byte[] result = new byte[length];
         for (int i = 0; i < length; i++) {
             int pos = i * 2;
@@ -98,19 +91,19 @@ public final class OpenGaussAuthenticationSCRAMSha256Packet implements PostgreSQ
         return result;
     }
     
-    private static byte charToByte(final char c) {
+    private byte charToByte(final char c) {
         return (byte) "0123456789ABCDEF".indexOf(c);
     }
     
     @SneakyThrows({NoSuchAlgorithmException.class, InvalidKeyException.class})
-    private static byte[] getKeyFromHmac(final byte[] key, final byte[] data) {
+    private byte[] getKeyFromHmac(final byte[] key, final byte[] data) {
         SecretKeySpec signingKey = new SecretKeySpec(key, HMAC_SHA256_ALGORITHM);
         Mac mac = Mac.getInstance(HMAC_SHA256_ALGORITHM);
         mac.init(signingKey);
         return mac.doFinal(data);
     }
     
-    private static String bytesToHexString(final byte[] src) {
+    private String bytesToHexString(final byte[] src) {
         StringBuilder result = new StringBuilder();
         for (byte each : src) {
             String hex = Integer.toHexString(each & 255);
@@ -126,10 +119,10 @@ public final class OpenGaussAuthenticationSCRAMSha256Packet implements PostgreSQ
     public void write(final PostgreSQLPacketPayload payload) {
         payload.writeInt4(AUTH_REQ_SHA256);
         payload.writeInt4(PASSWORD_STORED_METHOD_SHA256);
-        payload.writeBytes(random64Code);
-        payload.writeBytes(token);
+        payload.writeBytes(authHexData.getSalt().getBytes());
+        payload.writeBytes(authHexData.getNonce().getBytes());
         if (version < OpenGaussProtocolVersion.PROTOCOL_350.getVersion()) {
-            payload.writeBytes(serverSignature);
+            payload.writeBytes(serverSignature.getBytes());
         }
         if (OpenGaussProtocolVersion.PROTOCOL_351.getVersion() == version) {
             payload.writeInt4(serverIteration);
diff --git a/db-protocol/opengauss/src/test/java/org/apache/shardingsphere/db/protocol/opengauss/packet/authentication/OpenGaussAuthenticationSCRAMSha256PacketTest.java b/db-protocol/opengauss/src/test/java/org/apache/shardingsphere/db/protocol/opengauss/packet/authentication/OpenGaussAuthenticationSCRAMSha256PacketTest.java
index 043b9d23580..534c7a3b98f 100644
--- a/db-protocol/opengauss/src/test/java/org/apache/shardingsphere/db/protocol/opengauss/packet/authentication/OpenGaussAuthenticationSCRAMSha256PacketTest.java
+++ b/db-protocol/opengauss/src/test/java/org/apache/shardingsphere/db/protocol/opengauss/packet/authentication/OpenGaussAuthenticationSCRAMSha256PacketTest.java
@@ -33,7 +33,7 @@ public final class OpenGaussAuthenticationSCRAMSha256PacketTest {
     @Test
     public void assertWriteProtocol300Packet() {
         PostgreSQLPacketPayload payload = mock(PostgreSQLPacketPayload.class);
-        OpenGaussAuthenticationSCRAMSha256Packet packet = new OpenGaussAuthenticationSCRAMSha256Packet("", authHexData, OpenGaussProtocolVersion.PROTOCOL_350.getVersion() - 1, 2048);
+        OpenGaussAuthenticationSCRAMSha256Packet packet = new OpenGaussAuthenticationSCRAMSha256Packet(OpenGaussProtocolVersion.PROTOCOL_350.getVersion() - 1, 2048, authHexData, "");
         packet.write(payload);
         verify(payload).writeInt4(10);
         verify(payload).writeInt4(2);
@@ -45,7 +45,7 @@ public final class OpenGaussAuthenticationSCRAMSha256PacketTest {
     @Test
     public void assertWriteProtocol350Packet() {
         PostgreSQLPacketPayload payload = mock(PostgreSQLPacketPayload.class);
-        OpenGaussAuthenticationSCRAMSha256Packet packet = new OpenGaussAuthenticationSCRAMSha256Packet("", authHexData, OpenGaussProtocolVersion.PROTOCOL_350.getVersion(), 2048);
+        OpenGaussAuthenticationSCRAMSha256Packet packet = new OpenGaussAuthenticationSCRAMSha256Packet(OpenGaussProtocolVersion.PROTOCOL_350.getVersion(), 2048, authHexData, "");
         packet.write(payload);
         verify(payload).writeInt4(10);
         verify(payload).writeInt4(2);
@@ -57,7 +57,7 @@ public final class OpenGaussAuthenticationSCRAMSha256PacketTest {
     @Test
     public void assertWriteProtocol351Packet() {
         PostgreSQLPacketPayload payload = mock(PostgreSQLPacketPayload.class);
-        OpenGaussAuthenticationSCRAMSha256Packet packet = new OpenGaussAuthenticationSCRAMSha256Packet("", authHexData, OpenGaussProtocolVersion.PROTOCOL_351.getVersion(), 10000);
+        OpenGaussAuthenticationSCRAMSha256Packet packet = new OpenGaussAuthenticationSCRAMSha256Packet(OpenGaussProtocolVersion.PROTOCOL_351.getVersion(), 10000, authHexData, "");
         packet.write(payload);
         verify(payload).writeInt4(10);
         verify(payload).writeInt4(2);
diff --git a/proxy/frontend/opengauss/src/main/java/org/apache/shardingsphere/proxy/frontend/opengauss/authentication/OpenGaussAuthenticationEngine.java b/proxy/frontend/opengauss/src/main/java/org/apache/shardingsphere/proxy/frontend/opengauss/authentication/OpenGaussAuthenticationEngine.java
index 5906847d93e..8508d05344c 100644
--- a/proxy/frontend/opengauss/src/main/java/org/apache/shardingsphere/proxy/frontend/opengauss/authentication/OpenGaussAuthenticationEngine.java
+++ b/proxy/frontend/opengauss/src/main/java/org/apache/shardingsphere/proxy/frontend/opengauss/authentication/OpenGaussAuthenticationEngine.java
@@ -132,7 +132,7 @@ public final class OpenGaussAuthenticationEngine implements AuthenticationEngine
         ShardingSpherePreconditions.checkState(!Strings.isNullOrEmpty(username), EmptyUsernameException::new);
         serverIteration = startupPacket.getVersion() == OpenGaussProtocolVersion.PROTOCOL_350.getVersion() ? PROTOCOL_350_SERVER_ITERATOR : PROTOCOL_351_SERVER_ITERATOR;
         String password = rule.findUser(new Grantee(username, "%")).map(ShardingSphereUser::getPassword).orElse("");
-        context.writeAndFlush(new OpenGaussAuthenticationSCRAMSha256Packet(password, authHexData, startupPacket.getVersion(), serverIteration));
+        context.writeAndFlush(new OpenGaussAuthenticationSCRAMSha256Packet(startupPacket.getVersion(), serverIteration, authHexData, password));
         currentAuthResult = AuthenticationResultBuilder.continued(username, "", startupPacket.getDatabase());
         return currentAuthResult;
     }
diff --git a/proxy/frontend/opengauss/src/main/java/org/apache/shardingsphere/proxy/frontend/opengauss/authentication/authenticator/impl/OpenGaussSCRAMSha256PasswordAuthenticator.java b/proxy/frontend/opengauss/src/main/java/org/apache/shardingsphere/proxy/frontend/opengauss/authentication/authenticator/impl/OpenGaussSCRAMSha256PasswordAuthenticator.java
index c6e3fb90a04..a1876c5bef9 100644
--- a/proxy/frontend/opengauss/src/main/java/org/apache/shardingsphere/proxy/frontend/opengauss/authentication/authenticator/impl/OpenGaussSCRAMSha256PasswordAuthenticator.java
+++ b/proxy/frontend/opengauss/src/main/java/org/apache/shardingsphere/proxy/frontend/opengauss/authentication/authenticator/impl/OpenGaussSCRAMSha256PasswordAuthenticator.java
@@ -86,9 +86,8 @@ public final class OpenGaussSCRAMSha256PasswordAuthenticator implements OpenGaus
         if (null == rawHexString || rawHexString.isEmpty()) {
             return new byte[0];
         }
-        String hexString = rawHexString.toUpperCase(Locale.ENGLISH);
-        int length = hexString.length() / 2;
-        char[] hexChars = hexString.toCharArray();
+        int length = rawHexString.length() / 2;
+        char[] hexChars = rawHexString.toUpperCase(Locale.ENGLISH).toCharArray();
         byte[] result = new byte[length];
         for (int i = 0; i < length; i++) {
             int pos = i * 2;
diff --git a/proxy/frontend/opengauss/src/test/java/org/apache/shardingsphere/proxy/frontend/opengauss/authentication/OpenGaussAuthenticationEngineTest.java b/proxy/frontend/opengauss/src/test/java/org/apache/shardingsphere/proxy/frontend/opengauss/authentication/OpenGaussAuthenticationEngineTest.java
index c887f7ba991..7b26f11ab42 100644
--- a/proxy/frontend/opengauss/src/test/java/org/apache/shardingsphere/proxy/frontend/opengauss/authentication/OpenGaussAuthenticationEngineTest.java
+++ b/proxy/frontend/opengauss/src/test/java/org/apache/shardingsphere/proxy/frontend/opengauss/authentication/OpenGaussAuthenticationEngineTest.java
@@ -26,6 +26,8 @@ import lombok.SneakyThrows;
 import org.apache.shardingsphere.authority.config.AuthorityRuleConfiguration;
 import org.apache.shardingsphere.authority.rule.builder.AuthorityRuleBuilder;
 import org.apache.shardingsphere.db.protocol.constant.CommonConstants;
+import org.apache.shardingsphere.db.protocol.opengauss.constant.OpenGaussProtocolVersion;
+import org.apache.shardingsphere.db.protocol.opengauss.packet.authentication.OpenGaussAuthenticationHexData;
 import org.apache.shardingsphere.db.protocol.opengauss.packet.authentication.OpenGaussAuthenticationSCRAMSha256Packet;
 import org.apache.shardingsphere.db.protocol.payload.PacketPayload;
 import org.apache.shardingsphere.db.protocol.postgresql.payload.PostgreSQLPacketPayload;
@@ -42,6 +44,8 @@ import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
 import org.apache.shardingsphere.mode.metadata.persist.MetaDataPersistService;
 import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
 import org.apache.shardingsphere.proxy.frontend.authentication.AuthenticationResult;
+import org.apache.shardingsphere.proxy.frontend.authentication.Authenticator;
+import org.apache.shardingsphere.proxy.frontend.authentication.AuthenticatorFactory;
 import org.apache.shardingsphere.proxy.frontend.opengauss.ProxyContextRestorer;
 import org.apache.shardingsphere.proxy.frontend.opengauss.authentication.fixture.OpenGaussAuthenticationAlgorithm;
 import org.junit.Before;
@@ -50,6 +54,7 @@ import org.junit.runner.RunWith;
 import org.mockito.Answers;
 import org.mockito.ArgumentCaptor;
 import org.mockito.Mock;
+import org.mockito.MockedConstruction;
 import org.mockito.internal.configuration.plugins.Plugins;
 import org.mockito.junit.MockitoJUnitRunner;
 
@@ -60,8 +65,11 @@ import java.util.Properties;
 import static org.hamcrest.CoreMatchers.is;
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
 import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.mockConstruction;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
@@ -126,20 +134,48 @@ public final class OpenGaussAuthenticationEngineTest extends ProxyContextRestore
         Plugins.getMemberAccessor().set(OpenGaussAuthenticationEngine.class.getDeclaredField("startupMessageReceived"), target, true);
     }
     
+    @SuppressWarnings("rawtypes")
     @Test
     public void assertLoginSuccessful() {
-        assertLogin(password);
+        PostgreSQLPacketPayload payload = new PostgreSQLPacketPayload(createByteBuf(16, 128), StandardCharsets.UTF_8);
+        payload.writeInt4(64);
+        payload.writeInt4(OpenGaussProtocolVersion.PROTOCOL_350.getVersion());
+        payload.writeStringNul("user");
+        payload.writeStringNul(username);
+        payload.writeStringNul("client_encoding");
+        payload.writeStringNul("UTF8");
+        OpenGaussAuthenticationEngine engine = new OpenGaussAuthenticationEngine();
+        AuthenticationResult actual = engine.authenticate(channelHandlerContext, payload);
+        assertFalse(actual.isFinished());
+        assertThat(actual.getUsername(), is(username));
+        ArgumentCaptor<OpenGaussAuthenticationSCRAMSha256Packet> argumentCaptor = ArgumentCaptor.forClass(OpenGaussAuthenticationSCRAMSha256Packet.class);
+        verify(channelHandlerContext).writeAndFlush(argumentCaptor.capture());
+        OpenGaussAuthenticationSCRAMSha256Packet sha256Packet = argumentCaptor.getValue();
+        int serverIteration = getServerIteration(sha256Packet);
+        try (
+                MockedConstruction<AuthenticatorFactory> ignored = mockConstruction(AuthenticatorFactory.class, (mock, context) -> mockAuthenticatorFactory(mock))) {
+            payload = new PostgreSQLPacketPayload(createByteBuf(16, 128), StandardCharsets.UTF_8);
+            String clientDigest = encodeDigest(serverIteration, new OpenGaussAuthenticationHexData(), password);
+            payload.writeInt1('p');
+            payload.writeInt4(4 + clientDigest.length() + 1);
+            payload.writeStringNul(clientDigest);
+            actual = engine.authenticate(channelHandlerContext, payload);
+            assertTrue(actual.isFinished());
+        }
     }
     
-    @Test(expected = InvalidPasswordException.class)
-    public void assertLoginFailed() {
-        assertLogin("wrong" + password);
+    private void mockAuthenticatorFactory(final AuthenticatorFactory<?> mockedObject) {
+        Authenticator authenticator = mock(Authenticator.class);
+        when(authenticator.authenticate(any(), any())).thenReturn(true);
+        when(mockedObject.newInstance(any())).thenReturn(authenticator);
     }
     
-    private void assertLogin(final String inputPassword) {
+    @SuppressWarnings("rawtypes")
+    @Test(expected = InvalidPasswordException.class)
+    public void assertLoginFailed() {
         PostgreSQLPacketPayload payload = new PostgreSQLPacketPayload(createByteBuf(16, 128), StandardCharsets.UTF_8);
         payload.writeInt4(64);
-        payload.writeInt4(196608);
+        payload.writeInt4(OpenGaussProtocolVersion.PROTOCOL_350.getVersion());
         payload.writeStringNul("user");
         payload.writeStringNul(username);
         payload.writeStringNul("client_encoding");
@@ -151,38 +187,31 @@ public final class OpenGaussAuthenticationEngineTest extends ProxyContextRestore
         ArgumentCaptor<OpenGaussAuthenticationSCRAMSha256Packet> argumentCaptor = ArgumentCaptor.forClass(OpenGaussAuthenticationSCRAMSha256Packet.class);
         verify(channelHandlerContext).writeAndFlush(argumentCaptor.capture());
         OpenGaussAuthenticationSCRAMSha256Packet sha256Packet = argumentCaptor.getValue();
-        String random64Code = new String(getRandom64Code(sha256Packet));
-        String token = new String(getToken(sha256Packet));
         int serverIteration = getServerIteration(sha256Packet);
-        payload = new PostgreSQLPacketPayload(createByteBuf(16, 128), StandardCharsets.UTF_8);
-        String clientDigest = encodeDigest(inputPassword, random64Code, token, serverIteration);
-        payload.writeInt1('p');
-        payload.writeInt4(4 + clientDigest.length() + 1);
-        payload.writeStringNul(clientDigest);
-        actual = engine.authenticate(channelHandlerContext, payload);
-        assertThat(actual.isFinished(), is(password.equals(inputPassword)));
+        try (
+                MockedConstruction<AuthenticatorFactory> ignored = mockConstruction(AuthenticatorFactory.class,
+                        (mock, context) -> when(mock.newInstance(any())).thenReturn(mock(Authenticator.class)))) {
+            payload = new PostgreSQLPacketPayload(createByteBuf(16, 128), StandardCharsets.UTF_8);
+            String clientDigest = encodeDigest(serverIteration, new OpenGaussAuthenticationHexData(), "wrong" + password);
+            payload.writeInt1('p');
+            payload.writeInt4(4 + clientDigest.length() + 1);
+            payload.writeStringNul(clientDigest);
+            actual = engine.authenticate(channelHandlerContext, payload);
+            assertFalse(actual.isFinished());
+        }
     }
     
     private ByteBuf createByteBuf(final int initialCapacity, final int maxCapacity) {
         return new UnpooledHeapByteBuf(UnpooledByteBufAllocator.DEFAULT, initialCapacity, maxCapacity);
     }
     
-    @SneakyThrows(ReflectiveOperationException.class)
-    private byte[] getRandom64Code(final OpenGaussAuthenticationSCRAMSha256Packet packet) {
-        return (byte[]) Plugins.getMemberAccessor().get(OpenGaussAuthenticationSCRAMSha256Packet.class.getDeclaredField("random64Code"), packet);
-    }
-    
-    @SneakyThrows(ReflectiveOperationException.class)
-    private byte[] getToken(final OpenGaussAuthenticationSCRAMSha256Packet packet) {
-        return (byte[]) Plugins.getMemberAccessor().get(OpenGaussAuthenticationSCRAMSha256Packet.class.getDeclaredField("token"), packet);
-    }
-    
     @SneakyThrows(ReflectiveOperationException.class)
     private int getServerIteration(final OpenGaussAuthenticationSCRAMSha256Packet packet) {
         return (int) Plugins.getMemberAccessor().get(OpenGaussAuthenticationSCRAMSha256Packet.class.getDeclaredField("serverIteration"), packet);
     }
     
-    private String encodeDigest(final String password, final String random64code, final String token, final int serverIteration) {
-        return new String(OpenGaussAuthenticationAlgorithm.doRFC5802Algorithm(password, random64code, token, serverIteration));
+    private String encodeDigest(final int serverIteration, final OpenGaussAuthenticationHexData authHexData, final String password) {
+        return new String(OpenGaussAuthenticationAlgorithm.doRFC5802Algorithm(
+                new OpenGaussAuthenticationSCRAMSha256Packet(OpenGaussProtocolVersion.PROTOCOL_350.getVersion(), serverIteration, authHexData, password), password));
     }
 }
diff --git a/proxy/frontend/opengauss/src/test/java/org/apache/shardingsphere/proxy/frontend/opengauss/authentication/fixture/OpenGaussAuthenticationAlgorithm.java b/proxy/frontend/opengauss/src/test/java/org/apache/shardingsphere/proxy/frontend/opengauss/authentication/fixture/OpenGaussAuthenticationAlgorithm.java
index fdec185ef1b..6fdb3cd3541 100644
--- a/proxy/frontend/opengauss/src/test/java/org/apache/shardingsphere/proxy/frontend/opengauss/authentication/fixture/OpenGaussAuthenticationAlgorithm.java
+++ b/proxy/frontend/opengauss/src/test/java/org/apache/shardingsphere/proxy/frontend/opengauss/authentication/fixture/OpenGaussAuthenticationAlgorithm.java
@@ -20,6 +20,7 @@ package org.apache.shardingsphere.proxy.frontend.opengauss.authentication.fixtur
 import lombok.AccessLevel;
 import lombok.NoArgsConstructor;
 import lombok.SneakyThrows;
+import org.apache.shardingsphere.db.protocol.opengauss.packet.authentication.OpenGaussAuthenticationHexData;
 import org.apache.shardingsphere.db.protocol.opengauss.packet.authentication.OpenGaussAuthenticationSCRAMSha256Packet;
 import org.apache.shardingsphere.proxy.frontend.opengauss.authentication.authenticator.impl.OpenGaussSCRAMSha256PasswordAuthenticator;
 import org.mockito.internal.configuration.plugins.Plugins;
@@ -32,18 +33,16 @@ public final class OpenGaussAuthenticationAlgorithm {
     /**
      * Generate client digest.
      * 
+     * @param packet password
      * @param password password
-     * @param random64code random 64 code (salt)
-     * @param token token (nonce)
-     * @param serverIteration server iteration
      * @return client digest
      */
-    public static byte[] doRFC5802Algorithm(final String password, final String random64code, final String token, final int serverIteration) {
-        byte[] k = generateKFromPBKDF2(password, random64code, serverIteration);
-        byte[] clientKey = getKeyFromHmac(k, "Client Key".getBytes(StandardCharsets.UTF_8));
+    public static byte[] doRFC5802Algorithm(final OpenGaussAuthenticationSCRAMSha256Packet packet, final String password) {
+        byte[] k = generateKFromPBKDF2(packet, password);
+        byte[] clientKey = getKeyFromHmac(packet, k, "Client Key".getBytes(StandardCharsets.UTF_8));
         byte[] storedKey = sha256(clientKey);
-        byte[] tokenBytes = hexStringToBytes(token);
-        byte[] hmacResult = getKeyFromHmac(storedKey, tokenBytes);
+        byte[] tokenBytes = toHexBytes(packet, getAuthHexData(packet).getNonce());
+        byte[] hmacResult = getKeyFromHmac(packet, storedKey, tokenBytes);
         byte[] h = xor(hmacResult, clientKey);
         byte[] result = new byte[h.length * 2];
         bytesToHex(h, result, 0, h.length);
@@ -51,15 +50,19 @@ public final class OpenGaussAuthenticationAlgorithm {
     }
     
     @SneakyThrows(ReflectiveOperationException.class)
-    private static byte[] generateKFromPBKDF2(final String password, final String saltString, final int serverIteration) {
-        return (byte[]) Plugins.getMemberAccessor().invoke(OpenGaussAuthenticationSCRAMSha256Packet.class.getDeclaredMethod("generateKFromPBKDF2", String.class, String.class, int.class),
-                OpenGaussAuthenticationSCRAMSha256Packet.class, password, saltString, serverIteration);
+    private static OpenGaussAuthenticationHexData getAuthHexData(final OpenGaussAuthenticationSCRAMSha256Packet packet) {
+        return (OpenGaussAuthenticationHexData) Plugins.getMemberAccessor().get(OpenGaussAuthenticationSCRAMSha256Packet.class.getDeclaredField("authHexData"), packet);
     }
     
     @SneakyThrows(ReflectiveOperationException.class)
-    private static byte[] getKeyFromHmac(final byte[] key, final byte[] data) {
+    private static byte[] generateKFromPBKDF2(final OpenGaussAuthenticationSCRAMSha256Packet packet, final String password) {
+        return (byte[]) Plugins.getMemberAccessor().invoke(OpenGaussAuthenticationSCRAMSha256Packet.class.getDeclaredMethod("generateKFromPBKDF2", String.class), packet, password);
+    }
+    
+    @SneakyThrows(ReflectiveOperationException.class)
+    private static byte[] getKeyFromHmac(final OpenGaussAuthenticationSCRAMSha256Packet packet, final byte[] key, final byte[] data) {
         return (byte[]) Plugins.getMemberAccessor().invoke(
-                OpenGaussAuthenticationSCRAMSha256Packet.class.getDeclaredMethod("getKeyFromHmac", byte[].class, byte[].class), OpenGaussAuthenticationSCRAMSha256Packet.class, key, data);
+                OpenGaussAuthenticationSCRAMSha256Packet.class.getDeclaredMethod("getKeyFromHmac", byte[].class, byte[].class), packet, key, data);
     }
     
     @SneakyThrows(ReflectiveOperationException.class)
@@ -69,9 +72,8 @@ public final class OpenGaussAuthenticationAlgorithm {
     }
     
     @SneakyThrows(ReflectiveOperationException.class)
-    private static byte[] hexStringToBytes(final String rawHexString) {
-        return (byte[]) Plugins.getMemberAccessor().invoke(
-                OpenGaussAuthenticationSCRAMSha256Packet.class.getDeclaredMethod("hexStringToBytes", String.class), OpenGaussAuthenticationSCRAMSha256Packet.class, rawHexString);
+    private static byte[] toHexBytes(final OpenGaussAuthenticationSCRAMSha256Packet packet, final String rawHexString) {
+        return (byte[]) Plugins.getMemberAccessor().invoke(OpenGaussAuthenticationSCRAMSha256Packet.class.getDeclaredMethod("toHexBytes", String.class), packet, rawHexString);
     }
     
     @SneakyThrows(ReflectiveOperationException.class)