从MessageDigest到CryptoJS的摘要byteBuffer SHA512

时间:2018-12-17 17:46:36

标签: postman cryptojs sha512 message-digest

我尝试使用SHA512消化字节数组

Java:

MessageDigest sha512 = MessageDigest.getInstance("SHA-512");
System.out.println("sha512: "+Arrays.toString(sha512.digest(buffer.array())));

此示例使用输入进行计算:

[18, 52, 49, 114, -101, -17, 46, -74, -108, 84, 7, -37, -51, -87, -75, 123, 113, 71, -52, 109, -69, 6, 46, 19, -108, 100, -33, 14, 74, 122, -126, -103, 81, -93]

此输出:

[-6, -75, -91, -50, 3, 5, -78, -74, -63, -33, -103, 24, -18, 39, -45, -22, 30, -10, 58, -7, -2, -28, 77, 43, -78, 58, 123, -101, -12, 22, 63, 16, -56, -17, 58, 13, -26, 61, -45, -22, 100, 121, -118, -86, 53, 115, -42, 90, -70, 67, -61, 100, 54, -46, -113, -119, -95, 27, 23, -95, -76, 51, 52, 99]

我如何在邮递员上使用CryptoJS获得相同的结果?

谢谢-

1 个答案:

答案 0 :(得分:0)

这里有Java代码,我想将CryptoJS转换为javaScript并用邮递员作为脚本来计算SHA512哈希值:

    package hashtest;
import java.nio.ByteBuffer;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import com.google.common.io.BaseEncoding;

public class SpinHash
{
    private static byte[] applyChallenge(byte[] encrypted, byte[] challenge) throws NoSuchAlgorithmException {
        byte[] KEY_CHALLENGE_SEPARATOR = "".getBytes();   
        MessageDigest sha512 = MessageDigest.getInstance("SHA-512");

        ByteBuffer buffer = ByteBuffer.allocate(encrypted.length + challenge.length + KEY_CHALLENGE_SEPARATOR.length);
        buffer.put(encrypted);
        buffer.put(KEY_CHALLENGE_SEPARATOR);
        buffer.put(challenge);
        buffer.compact();

        return sha512.digest(buffer.array());
     }

     private static String returnHashPin(String spin, String challenge) throws NoSuchAlgorithmException
     {
         BaseEncoding BASE16 = BaseEncoding.base16();

         byte[] _challenge = BASE16.decode(challenge.toUpperCase());
         byte[] _SPIN = BASE16.decode(spin.toUpperCase());
         byte[] answer; 

         answer = applyChallenge(_SPIN, _challenge); 
         return BASE16.encode(answer).toUpperCase();
     }

    public static void main(String[] args) throws NoSuchAlgorithmException
    {
        System.out.println( "Hash-Wert:  " + returnHashPin("1234","089C25A81BA2B17601FB89A47CEE561737AA41055AFE88766883CA9C9E7545F5") );
    }

}