var crypto = require('crypto')
var hmac = function (key, content) {
var method = crypto.createHmac('sha1', key)
method.setEncoding('base64')
method.write(content)
method.end()
return method.read()
}
var hmac2 = function (key, content) {
return crypto.createHmac('sha1', key).update(content).digest().toString('base64')
//or
//return crypto.createHmac('sha1', key).update(content).digest('base64')
}
以上代码实现hmac哈希算法,它依赖于md5,sha1,sha256等其他hash算法,且需要一个key,这就更加安全了,上面两个函数实现一样的功能,只是调用形式不同。要查看crypto支持的哈希算法和加密算法,使用如下:
var allHashes = function () {
//console.log(crypto.getHashes())
return crypto.getHashes()
}
var allCiphers = function () {
//console.log(crypto.getCiphers())
return crypto.getCiphers()
}