npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@zhr6525/security-bcrypt

v0.2.10

Published

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

Downloads

4

Readme

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