可以使用sha1js函数来转换 也可以使用插件 推荐使用安装插件
npm install jshashes
var Hashes = require('jshashes')
// 字符串加密成 hex 字符串
function sha1 (s) {
let data = new Uint8Array(encodeUTF8(s))
let i, j, t
let l = ((data.length + 8) >>> 6 << 4) + 16
let sa = new Uint8Array(l << 2)
sa.set(new Uint8Array(data.buffer))
sa = new Uint32Array(sa.buffer)
for (t = new DataView(sa.buffer), i = 0; i < l; i++) sa[i] = t.getUint32(i << 2)
sa[data.length >> 2] |= 0x80 << (24 - (data.length & 3) * 8)
sa[l - 1] = data.length << 3
let w = []
let f = [
function () {
return m[1] & m[2] | ~m[1] & m[3]
},
function () {
return m[1] ^ m[2] ^ m[3]
},
function () {
return m[1] & m[2] | m[1] & m[3] | m[2] & m[3]
},
function () {
return m[1] ^ m[2] ^ m[3]
}
]
let rol = function (n, c) {
return n << c | n >>> (32 - c)
}
let k = [1518500249, 1859775393, -1894007588, -899497514]
let m = [1732584193, -271733879, null, null, -1009589776]
m[2] = ~m[0]
m[3] = ~m[1]
for (i = 0; i < s.length; i += 16) {
let o = m.slice(0)
for (j = 0; j < 80; j++) {
w[j] = j < 16 ? s[i + j] : rol(w[j - 3] ^ w[j - 8] ^ w[j - 14] ^ w[j - 16], 1)
t = rol(m[0], 5) + f[j / 20 | 0]() + m[4] + w[j] + k[j / 20 | 0] | 0
m[1] = rol(m[1], 30)
m.pop()
m.unshift(t)
}
for (j = 0; j < 5; j++) m[j] = m[j] + o[j] | 0
}
t = new DataView(new Uint32Array(m).buffer)
for (let i = 0; i < 5; i++) m[i] = t.getUint32(i << 2)
let hex = Array.prototype.map.call(new Uint8Array(new Uint32Array(m).buffer), function (e) {
return (e < 16 ? '0' : '') + e.toString(16)
}).join('')
return hex
}
function encodeUTF8 (s) {
const code = encodeURIComponent(s)
const bytes = []
for (var i = 0; i < code.length; i++) {
const c = code.charAt(i)
if (c === '%') {
const hex = code.charAt(i + 1) + code.charAt(i + 2)
const hexVal = parseInt(hex, 16)
bytes.push(hexVal)
i += 2
} else bytes.push(c.charCodeAt(0))
}
return bytes
}