2016-04-27 12 views
1

我想從它的加密庫中使用nodejs的createHmac函數。nodejs加密hmac產生不同的散列,如果祕密作爲文字與變量傳遞

問題:當給出(看似)相同的參數時,它會產生不同的哈希值。唯一的區別是'祕密'參數是字符串變量還是字符串文字。

以下SPA分離出這個問題。我正在使用nwjs(node webkit)SDK flavor v 0.14.2在OS X El Cap上運行此代碼。

任何幫助和建議感激地收到。

的index.html

<!DOCTYPE html> 
<html> 
<head> 
    <title>Context Menu</title> 
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" 
    rel="stylesheet" 
    integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" 
    crossorigin="anonymous"> 
</head> 
<body style="width: 100%; height: 100%;"> 

<div id="wrapper"> 
</div> 

<script src="https://code.jquery.com/jquery-2.2.3.min.js" 
    integrity="sha256-a23g1Nt4dtEYOj7bR+vTu7+T8VP13humZFBJNIYoEJo=" 
    crossorigin="anonymous"></script> 
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" 
    integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" 
    crossorigin="anonymous"></script> 
<script type="text/javascript" src="./index.js"></script> 
</body> 
</html> 

index.js

var nodeCrypto = require('crypto'); 

var payload = 'twas brillig and the slithy toves did gyre and gimble in the wabe'; 

// 
// simple UI to get a user-entered secret 
// and echo the results. 
// enter 'wibble' in input element to demo the problem to match hard coded literal 
// 
$('#wrapper').append (
    $('<div>').addClass('form-group') 
     .append (
      $('<label>').attr('for','userinput').text('Tell me a secret:'), 
      $('<input>').addClass('form-control').attr('type','text').attr('id','userinput') 
     ), 
    $('<p>').attr('id', 'hash'), 
    $('<p>').attr('id', 'nash') 
); 

$('input').on('change', function (ev) { 

    // compute hash based on user input 
    var hash = nodeCrypto.createHmac ('sha256', $(this).val()) 
     .update (payload) 
     .digest ('hex');    
    console.log ('hash: ' + hash); 
    $('p[id=hash]').text('secret: ' + $(this).val() + ', hash: ' + hash); 
    // logs hash: f7b4ae1aaa35b813571f00bca7c81d08176b56cb3a1d1f8c8ba95a17ba6f6f29 
    // as long as user enters 'wibble' 

    // compute hash based on string literal 
    var nash = nodeCrypto.createHmac ('sha256', 'wibble') 
       .update (payload) 
       .digest ('hex');    
    console.log ('nash: ' + nash); 
    $('p[id=nash]').text('secret: wibble, hash: ' + nash); 
    // logs hash: c9592948b3de038c9aa339f94b61928de803417183a6c95b1829a04c69fe6bf6 

}); 

Screengrab Shows user input together with computed hashes

的package.json

{ 
    "name": "hmac", 
    "main": "index.html", 
    "description": "nodejs crypto hmac test", 
    "author": "xxx" 
} 

回答

1

有一些關於隔離問題,寫的艱難的,但公平的人羣,是計算器,往往似乎全部由自己來搖出一個解決方案的說明的紀律。

因此,道歉爲此'問&回答'。我有一個解決方案,只是感到寬慰。經由

secret = new String(...)

  1. 迫使參數作爲一個單獨的物體引起的節點的密碼庫內部的失敗::

    進一步的實驗的一點點,得到這些見解

    TypeError: not a buffer.

    這是一條線索!

    在將用戶輸入轉換爲緩衝區之後將其作爲createHmac的祕密傳遞給緩衝區之後,會導致2個調用之間的一致行爲,因此

    更新JS

    // compute hash based on user input 
        var secretStr = $(this).val(); 
        var hash = nodeCrypto.createHmac ('sha256', secretStr) 
         .update (payload) 
         .digest ('hex');    
        console.log ('hash: ' + hash); 
        $('p[id=hash]').text('secret: ' + $(this).val() + ', hash: ' + hash); 
        // logs hash: f7b4ae1aaa35b813571f00bca7c81d08176b56cb3a1d1f8c8ba95a17ba6f6f29 
        // as long as user enters 'wibble' 
    
        // compute hash based on string literal 
        var nash = nodeCrypto.createHmac ('sha256', 'wibble') 
         .update (payload) 
         .digest ('hex');    
        console.log ('nash: ' + nash); 
        $('p[id=nash]').text('secret: wibble, nash: ' + nash); 
        // logs nash: c9592948b3de038c9aa339f94b61928de803417183a6c95b1829a04c69fe6bf6 
    
        // compute hash based on Buffer initialised from user input 
        var secretBuf = Buffer.from($(this).val()); 
        var mash = nodeCrypto.createHmac ('sha256', secretBuf) 
         .update (payload) 
         .digest ('hex');    
        console.log ('nash: ' + nash); 
        $('p[id=mash]').text('secret: wibble, mash: ' + nash); 
        // logs mash: c9592948b3de038c9aa339f94b61928de803417183a6c95b1829a04c69fe6bf6