2013-12-12 39 views
2

我在JavaScript編碼中面臨的問題,我沒有得到預期的結果這裏的JavaScript編碼工作不正常

這裏是我想列出下來 幾件事 - 1)當我在紅寶石做編碼,然後獲得預期的結果。 - 2),但是當我特林追蹤JavaScript相同的步驟,然後沒有得到預期的結果


1)請找這是工作的罰款紅寶石的代碼如下。

require 'openssl' 
require "base64" 
key = Base64.decode64("yZQungXDXtksG4Ypcl0f6moRXWTCHh/CYDViXnZvbC0=") 
data = "<content><app-id>9cd73144-46ca-4a02-852b-fa452b1d8bdc</app-id><hmac>HMACSHA256</hmac><signing-time>2013-11-19T12:06:11.409Z</signing-time></content>" 
result = OpenSSL::HMAC.digest('sha256', key , data) 
final_result = Base64.encode64(result) 

紅寶石輸出:收到預期的輸出

Key = "\xC9\x94.\x9E\x05\xC3^\xD9,\e\x86)r]\x1F\xEAj\x11]d\xC2\x1E\x1F\xC2`5b^vol-" 
result = "\xC4\x9B\x94\x9C\anQT\xF9';\xE9$\x1C\x98k\xEE)\xD77\xFD\xCA\a\xD1L\xBB\x9B\xD2r\xE9\x1A\xA8" 
final_result = "xJuUnAduUVT5JzvpJByYa+4p1zf9ygfRTLub0nLpGqg=\n" 

2)請找預期

  • 使用crypo.js這是不工作的JavaScript代碼如下用於編碼和解碼

<html> 
    <head> 
    <script src="http://crypto-js.googlecode.com/svn/tags/3.0.2/build/rollups/hmac-sha256.js"></script> 
    <script src="http://crypto-js.googlecode.com/svn/tags/3.0.2/build/components/enc-base64-min.js"></script> 

    <script> 
     key = window.atob("yZQungXDXtksG4Ypcl0f6moRXWTCHh/CYDViXnZvbC0="); 
     data = "<content><app-id>9cd73144-46ca-4a02-852b-fa452b1d8bdc</app-id><hmac>HMACSHA256</hmac><signing-time>2013-11-19T12:06:11.409Z</signing-time></content>"; 
     console.log("key"); 
     console.log(key); 
     result = CryptoJS.HmacSHA256(data, key); 
     console.log("result"); 
     console.log(result.toString()); 
     final_result = CryptoJS.enc.Base64.stringify(result); 
     console.log("final_result"); 
     console.log(final_result.toString()); 
    </script> 
    </head> 
    <body> 
    </body> 
</html> 

的Javascript輸出:收到預期的輸出

Key = "É.Ã^Ù,)r]êj]dÂÂ`5b^vol-" 
result = "035a028de6bea2c7843b4310b28b57f5193d7597840ea2f23c255cb889d77d60" 
final_result = "A1oCjea+oseEO0MQsotX9Rk9dZeEDqLyPCVcuInXfWA=" 

所以在這裏我不理解,爲什麼我在ruby and JavaScript

越來越keyresultfinal_result不同請分享你的經驗,它會幫助我很多

感謝

回答

-1

您可能需要使用Base64.strict_encode64(http://ruby-doc.org/stdlib-1.9.3/libdoc/base64/rdoc/Base64.html#method-i-strict_encode64) strict_encode完全符合RFC,而.encode不是。大多數實現不關心這個特定的問題(必須做w /換行字符),但有些可以。

+0

我擔心的是爲什麼我沒有得到與ruby的最終結果一樣的JavaScript'final_result'。你能解釋爲什麼這種差異? –

+0

那麼輸出是不完全相同的,因爲ruby使用十六進制打印輸出,因爲並非所有的字符都是ascii可打印的。 JavaScript顯然沒有檢測到,並且正在打印輸出。然而,僅僅通過觀察,我可以看出輸出結果並不相同,即使它們的打印方式不同。如果它們是相同的,那麼對於final_result,您仍然會得到相同的答案,因爲base64編碼的數據始終是ascii可打印的。 – kernelsmith

+0

問題的根源在於密鑰的不等價。儘管它們的打印方式不同,但可以通過比較第一個字符來判斷它們可能不一樣。十六進制C9(\ xC9)不是ascii可打印的(沒有超過7f)。如果你把它當作unicode,那麼第一個字符就是\ xC9 \ x94,根據[this](這是一個「向後的c」)(http://www.utf8-chartable.de/unicode-utf8-table.pl? start = 512&names = - &utf8 = string-literal)這不是js版本中的第一個字符。我的猜測仍然是一樣的,你需要在創建密鑰時使用Base64.strict_decode64 – kernelsmith

0

我試了很多,找到下面的解決方案。

<html> 
    <head> 
    <script src="http://crypto-js.googlecode.com/svn/tags/3.0.2/build/rollups/hmac-sha256.js"></script> 
    <script src="http://crypto-js.googlecode.com/svn/tags/3.0.2/build/components/enc-base64-min.js"></script> 
    <script src="jquery.base64.js"></script> 
    <script> 
     var key = Base64.decode("yZQungXDXtksG4Ypcl0f6moRXWTCHh/CYDViXnZvbC0="); 
     console.log(key) 
     words = CryptoJS.enc.Latin1.parse(key); 
     words = CryptoJS.enc.Hex.parse(words.toString()); 
    result = CryptoJS.HmacSHA256("<content><app-id>9cd73144-46ca-4a02-852b-fa452b1d8bdc</app-id><hmac>HMACSHA256</hmac><signing-time>2013-11-19T12:06:11.409Z</signing-time></content>", words); 
    console.log(result); 
    final_result = CryptoJS.enc.Base64.stringify(result); 
    console.log(final_result); 
    </script> 
</head> 
</html> 

現在我得到了預期的結果。

key = "É.Ã^Ù,)r]êj]dÂÂ`5b^vol-" 
result = "c49b949c076e5154f9273be9241c986bee29d737fdca07d14cbb9bd272e91aa8" 
final_result = "xJuUnAduUVT5JzvpJByYa+4p1zf9ygfRTLub0nLpGqg="