有沒有方法在Google Apps腳本中創建哈希值? Google Apps腳本將在.gs
代碼文件中運行服務器端代碼。 .gs
文件是用JavaScript編寫的。由於JavaScript主要是客戶端語言,並且對客戶端的任何內容進行加密都不安全,也許像HMAC for Javascript這樣的東西不可用?當我在hmac in javascript
上進行網絡搜索時,我得到的第一件東西是crypto-js
。但它看起來像我需要<script>
標籤鏈接到一些服務:通過Google Apps腳本使用HMAC方法生成鍵控哈希值
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/hmac-md5.js"></script>
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/hmac-sha1.js"></script>
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/hmac-sha256.js"></script>
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/hmac-sha512.js"></script>
<script>
var hash = CryptoJS.HmacMD5("Message", "Secret Passphrase");
var hash = CryptoJS.HmacSHA1("Message", "Secret Passphrase");
var hash = CryptoJS.HmacSHA256("Message", "Secret Passphrase");
var hash = CryptoJS.HmacSHA512("Message", "Secret Passphrase");
</script>
的Secret Passphrase
將是您的客戶端HTML。這沒有任何意義!哦!我剛剛在維基百科找到了一些僞代碼。
這是我在重構它的嘗試:
//blocksize is the size in bytes and is set to 64 bytes.
//byte size of any UTF-8 string
function byteCount(s) {
return encodeURI(s).split(/%..|./).length - 1;
};
function hmac(key, message) {
var blocksize = 64;
var keyLngth = byteCount(key);
if (keyLngth > blocksize) {
key = hash(key); // keys longer than blocksize are shortened
}
else if (keyLngth < blocksize) {
key = key + [0x00 * (blocksize - keyLngth)]; // keys shorter than blocksize are zero-padded
};
var o_key_pad = [0x5c * blocksize] ⊕ key; // Where blocksize is that of the underlying hash function
var i_key_pad = [0x36 * blocksize] ⊕ key; // Where ⊕ is exclusive or (XOR)
return hash(o_key_pad + hash(i_key_pad + message));
};
我想只要僞代碼規定:SHA-1,MD5,RIPEMD-128 /:以下散列函數hash(key)
一個160需要使用。
所以我做了SHA-1在JavaScript中搜索,發現這一點:
如何使用JavaScript創建一個HMAC值的任何信息,將不勝感激。與此同時,我可能會繼續努力。即使它是Javascript,它也是一個Google腳本代碼文件,可以在服務器上運行。