crypto模块
1.0、参考
1.1、crypto模块的作用

crypto模块提供了加密、解密、哈希等安全相关的算法。

1.2、crypto模块的加载
const crypto = require('crypto');
1.3、crypto模块的API
1.3.1、crypto.getHashes() -> String

用来查看支持哪些哈希算法。几乎支持了全部的你能想到的所有哈希算法。

1.3.2、crypto.createHash(algorithm : String) -> crypto.Hash

创建crypto.Hash的实例,algorithm必须是crypto.getHashes()中存在的。

示例:

function hash(algorithm, content) {
    const crypto = require('crypto');
    const hash = crypto.createHash(algorithm);
    hash.update(content);
    return hash.digest('hex');
}

function md5(content) {
    return hash('md5', content)
}

function sha256(content) {
    return hash('sha256', content)
}

const content = "ABCDE";

//MD5算法
const md5 = md5(content);
console.log("md5(" + content + ") = " + md5);
//md5(ABCDE) = 2ecdde3959051d913f61b14579ea136d

//SHA256算法
const sha256 = sha256(content);
console.log("sha256(" + content + ") = " + sha256);
//sha256(ABCDE) = f0393febe8baaa55e32f7be2a7cc180bf34e52137d99e056c817a9c07b8f239a