Perl SHA-256算法生成的输出与等效的Java不同

时间:2018-08-13 08:30:36

标签: java perl sha256

我有以下Java代码:

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;

public class MyClass {

    public static void main(String args[]) {

        byte[] salt = {
             84,  65,  -51,  83,   -4,  -17,  -32,  61,
            -26,  33, -106, -81,  -14,   70,  -30,  59,
             41, -19,   -1,  19, -104, -100,  -31,  31,
             57,  -6, -115, -99,    0,   38, -123, -11
        };

        byte[] password = { 100, 112, 107, 57, 52, 110, 50 };

        try {

            MessageDigest messageDigest;

            messageDigest = MessageDigest.getInstance("SHA-256");

            messageDigest.reset();
            messageDigest.update(salt);

            byte[] hash = messageDigest.digest(password);

            for (int i = 1; i < 1000; i++)
            {
                messageDigest.reset();
                hash = messageDigest.digest(hash);
            }

            StringBuffer stringBuffer = new StringBuffer(hash.length * 2);

            for (int i = 0; i < hash.length; i++)
            {
                stringBuffer.append(Character.forDigit(hash[i] >> 4 & 0xF, 16));
                stringBuffer.append(Character.forDigit(hash[i] & 0xF, 16));
            }

            System.out.println("HashArray: " + Arrays.toString(hash));
            System.out.println("HashString: " + stringBuffer.toString());
        }
        catch (NoSuchAlgorithmException e) {
          e.printStackTrace();
        }
    }
}

输出为

HashArray: [-127, -38, -46, -2, 51, -2, -83, -42, 3, 83, -78, -72, -115, -28, 108, 58, -76, -35, -13, 33, 63, -96, 104, 101, -76, 23, 102, -28, -60, 4, 85, 82]

HashString: 81dad2fe33feadd60353b2b88de46c3ab4ddf3213fa06865b41766e4c4045552

我试图将Java代码转换为Perl,但是我有一个理解上的问题。在Java中,我使用两个数组来生成哈希。如果我在Perl中使用相同的数组,则不会得到相同的结果。

这是我的Perl代码:

use strict;
use warnings;

use Digest::SHA 'sha256';

my @salt = (
     "84",  "65",  -"51",  "83",   "-4",  "-17",  "-32",  "61",
    "-26",  "33", "-106", "-81",  "-14",   "70",  "-30",  "59",
     "41", "-19",   "-1",  "19", "-104", "-100",  "-31",  "31",
     "57",  "-6", "-115", "-99",    "0",   "38", "-123", "-11"
);

my @password = ( "100", "112", "107", "57", "52", "110", "50" );

my $alg = 256;
my $sha = Digest::SHA->new($alg);

$sha->add(@salt);
$sha->add(@password);

for ( my $i = 1; $i < 1000; $i++ ) {
    $sha->add(@password)
}

my $output = $sha->hexdigest;
print "\nHash: $output\n";

输出为

Hash: 3883ae8f7438cc7e8fac86d25aa5789c4434294a70ea13d5e8f547fc7a8e45c2

有人可以解释如何获取Perl代码以产生与Java相同的输出吗?

1 个答案:

答案 0 :(得分:2)

两个问题:

  1. Java版本使用字节,Perl版本使用数字字符串。使用pack将字符串转换为等效的字节。

  2. 在Java版本中,您将哈希应用于到目前为止计算出的哈希,在Perl代码中,您一次又一次地对相同的值进行哈希。

#!/usr/bin/perl
use warnings;
use strict;

use Digest::SHA 'sha256';

my @salt = (84, 65, -51, 83, -4, -17, -32, 61, -26, 33, -106, -81,
            -14, 70, -30, 59, 41, -19, -1, 19, -104, -100, -31, 31,
            57, -6, -115, -99, 0, 38, -123, -11);

my @password = (100, 112, 107, 57, 52, 110, 50);

my $alg = 256;
my $sha = Digest::SHA->new($alg);

$sha->add(pack 'c*', @salt);
$sha->add(pack 'c*', @password);

$sha->add($sha->digest) for 1 .. 999;

my $output = $sha->hexdigest;
print "\nHash: $output\n";