1>> 检测是否支持 crypto 内置于node模块中
let crypto;
try {
crypto = require('crypto');
} catch (err) {
console.log('不支持 crypto');
}
2>>封装一个简单的md5加密方法
注意: 使用 require('crypto') 来访问该模块
const crypto = require('crypto')
function md5Crypto(password){
const hash = crypto.createHash('md5')
hash.update(password)
const md5Password = hash.digest('hex')
return md5Password;
}
module.exports = md5Crypto
PS:
如果要计算SHA1,只需要把'md5'改成'sha1'
hex为加密算法前后对比时, 一定要使用一致, 加密结果通常有两种表示方法:hex和base64
3>> 使用
import md5Crypto from './....'
const md5password= md5Crypto (password) //得到的胃md5加密后的密码