美文网首页
nodejs--crypto密码模块

nodejs--crypto密码模块

作者: 宋song一 | 来源:发表于2018-12-21 23:44 被阅读30次
  1. createHash
    适用于md4,md5,sha1,sha2(sha224,sha384,sha256,sha512)等
let hash = crypto.createHash('sha1');
let hash1 = crypto.createHash('md5');
let sha2 = crypto.createHash('sha256').update('IloveYou');
// const argvElement = process.argv[1];
// console.log(argvElement)
hash.update('IloveYou');
hash1.update("IloveYou");
console.log(hash.digest('hex'))
console.log(hash1.digest('hex'))
console.log(sha2.digest('hex'))

createHmac
Hmac可以理解为用随机数“增强”的哈希算法,加salt

let Hmd5 = crypto.createHmac('md5','hello').update('IloveYou');  
      console.log(Hmd5.digest('hex'))
  1. AES对称加密
    在AES的规格中,密钥长度只有128、192和256比特三种

128bit = 16字节

let data = '123456'
let password = 'baby'
const cipher  = crypto.createCipher('aes192', password)
let encrypted = cipher.update(data, 'utf-8', 'hex')
encrypted += cipher.final('hex')
console.log(encrypted);

nodejs声明crypto.createCipher()废弃,建议使用 crypto.createCipheriv() 代替

密码学分组模式推荐使用CBC(密文分组链接)和CTR(计数器)模式
分组模式默认为CBC,aes-128-cbc或aes128,初始化向量iv不可缺省
也可使用ctr模式,如:aes-128-ctr
加密结果通常有两种表示方法:hex和base64,这些功能Nodejs全部都支持,但是在应用中要注意,如果加解密双方一方用Nodejs,另一方用Java、PHP等其它语言,需要仔细测试。如果无法正确解密,要确认双方是否遵循同样的AES算法,字符串密钥和IV是否相同,加密后的数据是否统一为hex或base64格式。

const key = Buffer.from('Hello,PrettyGirl', 'utf8');
console.log(a===b)
let cipheriv = crypto.createCipheriv('aes-128-cbc',key,key);
let Cupdate = cipheriv.update('I love you','utf8','hex');
Cupdate+=cipheriv.final('hex')
console.log(Cupdate)

解密

const cipheriv = crypto.createDecipheriv('aes128', key, key);
let Cupdate = cipheriv.update('63fc3b6fb3513831c3189d9eee92a1ed','hex','utf8');
Cupdate+=cipheriv.final('utf8')
console.log(Cupdate)

相关文章

  • nodejs--crypto密码模块

    createHash适用于md4,md5,sha1,sha2(sha224,sha384,sha256,sha51...

  • 登录注册修改密码

    1.为什么要设计登录注册修改密码模块 2.登录注册修改密码模块分类 3.如何设计登录注册修改密码模块 4.登录注册...

  • Python入门教程系列:getpass

    getpass模块大概是标准库中最简单的一个模块了。getpass模块用于输入密码时,隐藏密码字符。 我们都知道,...

  • 忘记密码页面设计

    忘记密码: “忘记密码”模块设计包括两部分: ①重置密码页面 ②输入新密码页面

  • 1-4 需求分析实例

    电子商务网站的系统:用户模块;商品模块;订单模块;购物车模块;供应商模块。 用户模块: 1.包括属性:用户名、密码...

  • 任务161:Msf-弱点扫描

    用vnc模块爆破出密码之后,就可以用vnc连接工具去连接目标主机了。(vnc和远程连接相关) VNC无密码访问模块...

  • 46-创建用户,设置随机密码

    randpass模块参见《37-生成密码/验证码》

  • 设计前需要思考的方方面面

    账号:18658145003 密码:123456 一、需要参考的思路: 一、原料订单需要涉及的模块: a、订单模块...

  • 移动端-全局说明

    页面模块与全局交互 1、页面模块 页面按照模块分为首页、账户中心、充值/提现、登录/注册/找回密码、用户投资 各个...

  • Laravel框架 之 ResetPassword

    本文的示例代码参考resetpassword 目录 开始 Auth模块 配置邮箱 重置密码 源码剖析生成重置密码t...

网友评论

      本文标题:nodejs--crypto密码模块

      本文链接:https://www.haomeiwen.com/subject/znnvkqtx.html