Archived Forum Post

Index of archived forum posts

Question:

Passing byte[] in Chilkat Java API

Dec 21 '16 at 12:36

I have a working hashing task in VB.NET as follows:

    Dim operand As Byte() = some bytes....
    Dim ckCrypt As New Chilkat.Crypt2()
    ckCrypt.EncodingMode = "Base64"
    ckCrypt.HashAlgorithm = "sha1"
    Dim lNewDigest As String = ckCrypt.HashBytesENC(operand)

This works perfectly. The variable lNewDigest gets the Digest value as expected.

Now I have a need to have the same task in Android.

So my code is:

    byte[] operand= some bytes....
    CkCrypt2 ckCrypt = new CkCrypt2();
    ckCrypt.put_EncodingMode( "Base64");
    ckCrypt.put_HashAlgorithm ( "sha1");
    String lNewDigest = ckCrypt.HashBytesENC(operand);

I saw some example on using CkByteData in between. But I am totally lost. I did not have any luck in appending plain bytes (operand) to CkByteData.


Answer

I completely understand why you are lost -- it's entirely confusing and there are plans for the future to rid ourselves of needing to use CkByteData (and CkString)..

For now though, here's the solution:

CkCrypt2 crypt = new CkCrypt2();

// Choose the hash algorithm. // Can be "sha1", "sha256", "sha384", "sha512", "md2", "md5", "haval", "ripemd128", "ripemd160","ripemd256", or "ripemd320". crypt.put_HashAlgorithm("sha1");

CkByteData byteData = new CkByteData(); byte[] myData = "Any String you want".getBytes(); byteData.appendByteArray(myData);

String sha1Hash = crypt.hashBytesENC(byteData); System.out.println(sha1Hash);