大佬教程收集整理的这篇文章主要介绍了Node.js基本模块crypto(加解密),大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
const crypto = require('crypto'); const hash = crypto.createHash('md5'); // 可任意多次调用update(): hash.update('Hello,world!'); hash.update('Hello,nodejs!'); console.log(hash.digest('hex')); // 7e1977739c748beac0c0fd14fd26a544
const crypto = require('crypto'); const hmac = crypto.createHmac('sha256','secret-key'); hmac.update('Hello,world!'); hmac.update('Hello,nodejs!'); console.log(hmac.digest('hex')); // 80f7e22570...
const crypto = require('crypto'); function aesEncrypt(data,key) { const cipher = crypto.createCipher('aes192',key); var crypted = cipher.update(data,'utf8','hex'); crypted += cipher.final('hex'); return crypted; } function aesDecrypt(encrypted,key) { const decipher = crypto.createDecipher('aes192',key); var decrypted = decipher.update(encrypted,'hex','utf8'); decrypted += decipher.final('utf8'); return decrypted; } var data = 'Hello,this is a secret message!'; var key = 'password!'; var encrypted = aesEncrypt(data,key); var decrypted = aesDecrypt(encrypted,key); console.log('Plain text: ' + data); console.log('Encrypted text: ' + encrypted); console.log('Decrypted text: ' + decrypted);
const crypto = require('crypto'); // xiaoming's keys: var ming = crypto.createDiffieHellman(512); var ming_keys = ming.generateKeys(); var prime = ming.getPrime(); var generator = ming.getGenerator(); console.log('Prime: ' + prime.toString('hex')); console.log('Generator: ' + generator.toString('hex')); // xiaohong's keys: var hong = crypto.createDiffieHellman(prime,generator); var hong_keys = hong.generateKeys(); // exchange and generate secret: var ming_secret = ming.computeSecret(hong_keys); var hong_secret = hong.computeSecret(ming_keys); // print secret: console.log('Secret of Xiao Ming: ' + ming_secret.toString('hex')); console.log('Secret of Xiao Hong: ' + hong_secret.toString('hex'));
--- end ---
以上是大佬教程为你收集整理的Node.js基本模块crypto(加解密)全部内容,希望文章能够帮你解决Node.js基本模块crypto(加解密)所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。