文檔地址:http://nodejs.cn/api/crypto.html
使用crypto.createHash(algorithm [,options])
這個(gè)方法贺拣,該創(chuàng)建并返回一個(gè)Hash對(duì)象靖榕,該對(duì)象可用于使用給定的哈希摘要生成哈希摘要algorithm
标锄。其中algorithm
取決于平臺(tái)上OpenSSL版本支持的可用算法。
const {createHash}= require('crypto');
/**
* @param {string} algorithm
* @param {any} content
* @return {string}
*/
const encrypt = (algorithm, content) => {
let hash = createHash(algorithm)
hash.update(content)
return hash.digest('hex')
}
/**
* @param {any} content
* @return {string}
*/
const sha1 = (content) => encrypt('sha1', content)
/**
* @param {any} content
* @return {string}
*/
const md5 = (content) => encrypt('md5', content)
module.exports={sha1,md5,encrypt}
下面是使用es6的方法進(jìn)行導(dǎo)出
import {createHash} from 'crypto'
/**
* @param {string} algorithm
* @param {any} content
* @return {string}
*/
export const encrypt = (algorithm, content) => {
let hash = createHash(algorithm)
hash.update(content)
return hash.digest('hex')
}
/**
* @param {any} content
* @return {string}
*/
export const sha1 = (content) => encrypt('sha1', content)
/**
* @param {any} content
* @return {string}
*/
export const md5 = (content) => encrypt('md5', content)
export default encrypt