B4J Question [solved] different HmacSHA256 results using javaobject and using java inline code

xulihang

Active Member
Licensed User
Longtime User
I am trying to call this machine translation api: http://ai.sogou.com/ai-docs/api/sign. But I come across a problem generating the signature.

So I decided to use inline java code to do this.

Putting the arguments directly in this java code to call is fine and returns the right result: vuVEkzcnUeFv8FxeWS50c7S0HaYH1QKgtIV5xrxDY/s=

B4X:
sha256_HMAC("sac-auth-v1/bTkALtTB9x6GAxmFi9wetAGH/1491810516/3600\nPOST\napi.ai.sogou.com\n/speech/asr\nidx=1&starttime=1491810516&type=gbk","PMROwlieALT36qfdGClVz2iH4Sv8xZxe");

But when passing the arguments using javaobject, the result is different: zgXLSfsTi/WMWO7OWQhWLWPOk8jeS+nEWuoPB8KLMz8=

B4X:
Sub getSignatureUsingInlineJava(prefix As String,secretKey As String,params As String) As String
    Dim combined As String
    combined=$"sac-auth-v1/bTkALtTB9x6GAxmFi9wetAGH/1491810516/3600\nPOST\napi.ai.sogou.com\n/speech/asr\nidx=1&starttime=1491810516&type=gbk"$
    secretKey=$"PMROwlieALT36qfdGClVz2iH4Sv8xZxe"$
    Dim jo As JavaObject
    jo=asJO(Me)
    Return jo.RunMethod("getSignature",Array As String(combined,secretKey))
End Sub

Sub getSignature(prefix As String,secretKey As String,params As String) As String
  
    Dim mactool As Mac
    Dim k As KeyGenerator
    k.Initialize("HmacSHA256")

    Dim combined As String=prefix&"\nPOST\napi.ai.sogou.com\n/pub/nlp/fanyi\n"&params
    combined=$"sac-auth-v1/bTkALtTB9x6GAxmFi9wetAGH/1491810516/3600\nPOST\napi.ai.sogou.com\n/speech/asr\nidx=1&starttime=1491810516&type=gbk"$
    secretKey=$"PMROwlieALT36qfdGClVz2iH4Sv8xZxe"$
    k.KeyFromBytes(secretKey.GetBytes("UTF-8"))
    mactool.Initialise("HmacSHA256",k.Key)
    mactool.Update(combined.GetBytes("UTF-8"))
    Dim bb() As Byte
    bb=mactool.Sign
    Dim base As Base64
    Dim sign As String=base.EncodeBtoS(bb,0,bb.Length)
    Log("sign"&sign)
    Return sign
End Sub

Sub getMap(key As String,parentmap As Map) As Map
    Return parentmap.Get(key)
End Sub

Sub asJO(o As JavaObject) As JavaObject
    Return o
End Sub

#If JAVA

import java.nio.charset.StandardCharsets;

import javax.crypto.Mac;

import javax.crypto.spec.SecretKeySpec;

public static String getSignature(String message, String secretKey){
    // return sha256_HMAC("sac-auth-v1/bTkALtTB9x6GAxmFi9wetAGH/1491810516/3600\nPOST\napi.ai.sogou.com\n/speech/asr\nidx=1&starttime=1491810516&type=gbk","PMROwlieALT36qfdGClVz2iH4Sv8xZxe");
    return sha256_HMAC(message,secretKey);
}

public static String sha256_HMAC(String message, String secretKey) {
    System.out.println(message);
    System.out.println(secretKey);
    byte[] secret;
    secret=secretKey.getBytes(StandardCharsets.UTF_8);
    String hash = "";

    try {

        Mac sha256_HMAC = Mac.getInstance("HmacSHA256");

        SecretKeySpec secret_key = new SecretKeySpec(secret, "HmacSHA256");

        sha256_HMAC.init(secret_key);

        byte[] bytes = sha256_HMAC.doFinal(message.getBytes(StandardCharsets.UTF_8));

        hash = new String(java.util.Base64.getEncoder().encode(bytes), StandardCharsets.UTF_8);

        System.out.println(hash);

    } catch (Exception e) {

        System.out.println("Error HmacSHA256 ===========" + e.getMessage());

    }

    return hash;

}
#End If
 
Last edited:

xulihang

Active Member
Licensed User
Longtime User
Sorry that I put up the question so quickly. I just remember that the "\n" in Java means CRLF in B4J.

The api document only uses PHP as an example
 
Upvote 0
Top