有没有办法在 Google Apps 脚本中创建哈希值?Google Apps 脚本将在.gs
代码文件中运行服务器端代码。该.gs
文件是用 JavaScript 编写的。因为 JavaScript 主要是一种客户端语言,并且加密客户端的任何内容都不安全,所以可能无法使用诸如用于 Javascript 的 HMAC 之类的东西?当我进行网络搜索时hmac in javascript
,我得到的第一件事是crypto-js
. 但看起来我需要链接到<script>
标签中的一些服务:
<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));
};
我猜在伪代码声明的任何地方:hash(key)
需要使用以下哈希函数之一:SHA-1、MD5、RIPEMD-128/160。
所以我在 JavaScript 中搜索了 SHA-1,发现了这个:
任何有关如何使用 Javascript 创建 HMAC 值的信息将不胜感激。在此期间,我可能会继续努力。尽管它是 Javascript,但它是.gs
在服务器上运行的 Google Apps 脚本代码文件。