0

HMACSHA1一方向アルゴリズムを使用しようとしています

これは私のコードです

@Test
public void encodeTest() {

    String HMAC_SHA1_ALGORITHM = "HmacSHA1";
    String EXPECTED_BASE_64 = "g9OrJ8pQNYprnXuBPFXcirrqpxE=";

    String text = "encodeme";
    String result;

    try {
        SecretKeySpec signingKey = new SecretKeySpec(
                "MSbN2crsrdTEsLetTixpV46q+fTZotdZjwoEpO62vYk=".getBytes(),
                HMAC_SHA1_ALGORITHM);
        // Get an hmac_sha1 Mac instance and initialise with the signing key
        Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
        mac.init(signingKey);
        // Compute the hmac
        byte[] rawHmac = mac.doFinal(text.getBytes());
        // Convert raw bytes to Hex
        byte[] hexBytes = new Hex().encode(rawHmac);
        // Covert array of Hex bytes to a String

        result = new String(hexBytes, "ISO-8859-1");
        // Ok, this matches with the web
        System.out.println("HEX:" + result);

        String encodedBase64 = new String(Base64.encodeBase64(hexBytes));
        System.out.println("BASE64:" + encodedBase64);

        // In the web i get a smaller chain, why?
        System.out.println("EXPECTED BASE64:" + EXPECTED_BASE_64);
        Assert.assertEquals(EXPECTED_BASE_64, encodedBase64);

    } catch (InvalidKeyException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

出力は

HEX:83d3ab27ca50358a6b9d7b813c55dc8abaeaa711 BASE64:ODNkM2FiMjdjYTUwMzU4YTZiOWQ3YjgxM2M1NWRjOGFiYWVhYTcxMQ ==

期待されるBASE64:g9OrJ8pQNYprnXuBPFXcirrqpxE =

私はこれらのオンラインサイトを使用して私の期待を定義しました

http://hash.online-convert.com/sha1-generator

text = encodeme

共有秘密鍵の使用

key = MSbN2crsrdTEsLetTixpV46q + fTZotdZjwoEpO62vYk =

私が得る結果はわずかに異なります

hex:83d3ab27ca50358a6b9d7b813c55dc8abaeaa711 HEX:83D3AB27CA50358A6B9D7B813C55DC8ABAEAA711 h:e:x:83:d3:ab:27:ca:50:35:8a:6b:9d:7b:81:3c:55:dc 11 base64:g9OrJ8pQNYprnXuBPFXcirrqpxE =

ご覧のとおり、16進出力は100%に一致しますが、base64出力はまったく一致しません。

問題がそのサイトにないことを確認するために、base64を使用して16進文字列をエンコードするために別のサイトに移動しましたが、同じ結果が得られました...

http://tomeko.net/online_tools/hex_to_base64.php?lang=en

なぜこれが起こっているのか誰かが知っていますか?

前もって感謝します!

4

0 に答える 0