0.2.12 • Published 3 months ago

security-bcrypt v0.2.12

Weekly downloads
-
License
MIT
Repository
-
Last release
3 months ago

security-bcrypt

这是一个node加解密的库。 包括非对称加密,对称加密,盐和比较,流式加解密。

install

npm i security-bcrypt

本地测试

1.环境搭建 安装node 建议安装:v14版本,node-v14.18.2-x64 测试是否安装成功用 node -v

npm install -g typescript npm install -g ts-node

2.安装npm 依赖 npm i

3.demo测试 ts-node demo.ts

4.使用

const crypto = require('crypto')
import { getHas, compare, getHasSync,compareSync,encrypt,decrypt,encryptStream,decryptStream } from 'security-bcrypt'
//encrypt(data, type, key,) 和 decrypt(data, type, key,) 
//其中data为要加密的数据,必填
//type为加密类型,可选值为:'rsa'(非对称加密),  'aes-cbc'(对称加密),'aes-ecb'(对称加密), 'des-cbc'(对称加密),'des-ecb'(对称加密) 选填,默认值为'aes-cbc'
//key为加解密的key,选填

//1.非对称加密,加密每次生成的密文不一样
function ras() {
    let encryptMsg = encrypt('gaga','rsa');
    console.log('1.RSA非对称加密,加密后密文为 ',encryptMsg)

    let tempencryptMsg = 'Rg3DR225v/Osrgn6flgzIllyqgnZhGSDZuUFQUfTtByo0vJiUHdOqSmbawqIiLadVwxoWEZZYmWsXP3hWCTMskkr2Vw7z21FA4BDRuw6Z7qvEBugT5SlpiDNkVHt5VhoYQW14pnVwsN4ZWndo91pnnpdTnX/i5GCtFNdRYHknsc='
    let decryptMsg = decrypt(tempencryptMsg,'rsa');

    console.log('RSA解密后,明文为 ',decryptMsg)
    console.log('')
}
ras()
//2.对称加密,每次生成密文一样
function aes() {
    let encryptMsg = encrypt('gaga','des-ecb'); //encrypt('gaga')等同于encrypt('gaga','aes-cbc');
    console.log('2.des-ecb对称加密, 密文为 ',encryptMsg)
    let decryptMsg = decrypt(encryptMsg,'des-ecb');

    console.log('des-ecb解密后 is ',decryptMsg)
    console.log('')
}
aes()

//3.1异步调用,getHas和compare,每次加盐后,hash都不一样
// async function asyncSaltDemo() {
//     let hash = await getHas('iamapassword')
//     let result = await compare('iamapassword',hash)
//     console.log('3.compare result is ',result)
// }

//3.2同步调用,getHas和compare
function syncSaltDemo() {
    let hashSync = getHasSync('iamapassword')
    console.log('3. 每次加盐后,hash都不一样 ',hashSync)
    let resultSync = compareSync('iamapassword',hashSync)
    console.log('密码和密文比较结果为 ',resultSync)
    console.log('')
}
syncSaltDemo()
// asyncSaltDemo()


//4. 流式加解密  key base64

async function encryptData() {
    // let data = 'hi shuangxu!'
    // console.log('加密前 string is ',data.toString('hex'))//hi shuangxu!
  
    let data =  Buffer.from('hi shuangxu!')
    console.log('4.流式加密,加密前 16进制 is ',data.toString('hex'))//686920736875616e67787521
    const key = Buffer.from('UZ/1c0zuAqURlFKd0/7+TtXP4aFPugihjem1Efiz2ew=', 'base64')
  
    let cipherGCMTypes = 'aes-256-gcm' //cipherGCMTypes可选类型为'aes-128-gcm' | 'aes-192-gcm' | 'aes-256-gcm'
    let result = await encryptStream(data, cipherGCMTypes,key,)
    console.log('加密后buffer为 ',result) //<Buffer 30 1b a9 16 d4 c3 9d 59 37 0d 27 ea 3b 82 10 aa b2 90 ea dc e9 71 b8 af 12 b7 3c 3f 45 11 42 6a e2 8e 85 bc 2f 19 38 e4>
    // const buffer = Buffer.from(result, 'hex');
  
    let result2 = await decryptStream(result,cipherGCMTypes,key)
    console.log('解密后 is ',result2)//686920736875616e67787521
  }
  encryptData()

5.输出为

1.RSA非对称加密,每次生成的密文不一样,加密后密文为  VbT/VD2BLYO6K/9l927IhPgyiW27rtNRzGQ4IlZsVuAaIrOy5LOcq17qj6wlSspFNDJQC2c6M2p3/egdDNPl26oghM+FcPUATaARcB1EDhLvCH+TucMD9ReVorRrv+zyfnI6SAKclczD9x3VMj2lP18Z0q22Jcti/rynZhS84oc=
RSA解密后,明文为  gaga

2.des-ecb对称加密, 密文为  rag9GSs1H/I=
des-ecb解密后 is  gaga

3. 每次加盐后,hash都不一样  $2a$10$ftKqC2kJTZgYckEckxmTyeiO8Jr5gdo//O7EWOwr8tQNvpAnB8LBm
密码和密文比较结果为  true

4.流式加密,加密前 16进制 is  686920736875616e67787521
加密后buffer为  <Buffer 30 1b a9 16 d4 c3 9d 59 37 0d 27 ea 3b 82 10 aa b2 90 ea dc e9 71 b8 af 12 b7 3c 3f 45 11 42 6a e2 8e 85 bc 2f 19 38 e4>
解密后 is  686920736875616e67787521
0.2.12

3 months ago

0.2.11

5 months ago

0.2.10

6 months ago

0.2.9

9 months ago

0.2.8

9 months ago

0.2.7

9 months ago

0.2.6

9 months ago

0.2.5

9 months ago

0.2.4

9 months ago

0.2.3

9 months ago

0.2.2

9 months ago

0.2.1

10 months ago

0.2.0

10 months ago

0.1.9

10 months ago

0.1.8

10 months ago

0.1.7

10 months ago

0.1.6

10 months ago

0.1.5

10 months ago

0.1.4

10 months ago

0.1.3

10 months ago

0.1.2

10 months ago

0.1.1

10 months ago

0.1.0

10 months ago

0.0.9

10 months ago

0.0.8

10 months ago

0.0.7

10 months ago

0.0.6

10 months ago

0.0.5

10 months ago

0.0.4

10 months ago

0.0.3

10 months ago

0.0.2

10 months ago