decrypt-core
TypeScript icon, indicating that this package has built-in type declarations

1.2.2 • Public • Published

decrypt-core

NPM version NPM downloads

基于 3DES 算法的数据加解密核心库

特性

  • 使用简单-提供加密、解密、签名函数
  • 稳定性高-公司真实项目接入使用
  • 支持多环境-支持 Node.js、web 浏览器、各种小程序、RN 及 weex 跨平台框架

使用

数据加解密

全局对外提供了两个核心方法 encryptdecrypt,引入项目后直接使用即可

  • ESM使用

    在支持ES6 Module时可使用import导入

    import { encrypt, decrypt } from 'decrypt-core'
    const key = '15e89af5da164202a4f839a2f1e7320a'
    const data = '{"page":1,"size":10}'
    const encData = encrypt(data, key)
    const decData = decrypt(encData, key)
    console.log(`加密后数据:${encData}\n解密后数据:${decData}`)
  • CJS使用

    const { encrypt, decrypt } = require('decrypt-core')
    const key = '15e89af5da164202a4f839a2f1e7320a'
    const data = '{"page":1,"size":10}'
    const encData = encrypt(data, key)
    const decData = decrypt(encData, key)
    console.log(`加密后数据:${encData}\n解密后数据:${decData}`)
  • UMD使用

    使用script标签引入dist/index.umd.min.js文件后,模块导出的全局变量在window.decryptCore

    <script src="https://unpkg.com/decrypt-core@latest/dist/index.umd.min.js"></script>
    <script>
      var key = '15e89af5da164202a4f839a2f1e7320a'
      const data = '{"page":1,"size":10}'
      const encData = window.decryptCore.encrypt(data, key)
      const decData = window.decryptCore.decrypt(encData, key)
      console.log(`加密后数据:${encData}\n解密后数据:${decData}`)
    </script>

校验加密数据格式

全局对外提供了工具方法 isEncryptedData ,可用于检查数据是否是数据加密方法 encrypt 返回的数据格式

import { isEncryptedData } from 'decrypt-core'

isEncryptedData('123445') // false 不是加密数据格式

isEncryptedData('93468187855434817844048812694314B1382F05951542D6B98311D90CD0B97E22E6D052DE6A9B83381E97E8B23AC5209F8D4E6428C697EAEFEB495FCF7673E48E4D7087A2B24CEAFE127793421DAB91FCD411D04B85BCC5427DB76E6D3353BE8897BE1DAE3D28DBDF053D7707BACF0AC77CCF0426BA8F76E9FC578D8D91803289F53AD66A70AF73B0756B97F314D33997191E8E976EDFAFA46A75CC393A88B1') // true 是加密数据格式

加签验签

v1.2.0 及以上版本支持

全局对外提供两个签名相关方法 createSignverifySign,可用于对数据进行加签、验签

import { createSign, verifySign } from 'decrypt-core'
const data = { page: 1, size: 10 }
const key = '15e89af5da164202a4f839a2f1e7320a'
const sign = createSign(data, key) // 返回签名字符串
const dataSigned1 = { ...data, sign: 'aaaa' }
const dataSigned2 = { ...data, sign }
verifySign(data, key) // 报错:signature field not found
verifySign(dataSigned1, key) // false 验签未通过
verifySign(dataSigned2, key) // true 验签通过

相关项目

NPM脚本

  • lint-代码格式检查
  • test-执行测试用例
  • test:coverage-统计测试覆盖率
  • coverage-统计测试覆盖率并在 coverage 目录生成结果详情
  • build-使用father进行打包
  • anaysis-使用当前机器进行加密比率、速率分析
  • anaysis:write-使用当前机器进行加密比率、速率分析并写入结果到 README.md

压缩率

npm run anaysis

[
  {
    "length": "2byte",
    "encLength": "64byte",
    "time": "4ms",
    "ratio": "3200.0000%",
    "type": "英文"
  },
  {
    "length": "100byte",
    "encLength": "256byte",
    "time": "5ms",
    "ratio": "256.0000%",
    "type": "英文"
  },
  {
    "length": "1000byte",
    "encLength": "2048byte",
    "time": "10ms",
    "ratio": "204.8000%",
    "type": "英文"
  },
  {
    "length": "10000byte",
    "encLength": "20064byte",
    "time": "101ms",
    "ratio": "200.6400%",
    "type": "英文"
  },
  {
    "length": "50000byte",
    "encLength": "100064byte",
    "time": "283ms",
    "ratio": "200.1280%",
    "type": "英文"
  },
  {
    "length": "100000byte",
    "encLength": "200064byte",
    "time": "510ms",
    "ratio": "200.0640%",
    "type": "英文"
  },
  {
    "length": "3byte",
    "encLength": "64byte",
    "time": "11ms",
    "ratio": "2133.3333%",
    "type": "中文"
  },
  {
    "length": "102byte",
    "encLength": "480byte",
    "time": "2ms",
    "ratio": "470.5882%",
    "type": "中文"
  },
  {
    "length": "1002byte",
    "encLength": "4064byte",
    "time": "13ms",
    "ratio": "405.5888%",
    "type": "中文"
  },
  {
    "length": "10002byte",
    "encLength": "40000byte",
    "time": "115ms",
    "ratio": "399.9200%",
    "type": "中文"
  },
  {
    "length": "50001byte",
    "encLength": "199712byte",
    "time": "479ms",
    "ratio": "399.4160%",
    "type": "中文"
  },
  {
    "length": "100002byte",
    "encLength": "399296byte",
    "time": "922ms",
    "ratio": "399.2880%",
    "type": "中文"
  },
  {
    "length": "5byte",
    "encLength": "160byte",
    "time": "1ms",
    "ratio": "3200.0000%",
    "type": "中英混合"
  },
  {
    "length": "100byte",
    "encLength": "2048byte",
    "time": "5ms",
    "ratio": "2048.0000%",
    "type": "中英混合"
  },
  {
    "length": "1000byte",
    "encLength": "20032byte",
    "time": "55ms",
    "ratio": "2003.2000%",
    "type": "中英混合"
  },
  {
    "length": "10000byte",
    "encLength": "199968byte",
    "time": "502ms",
    "ratio": "1999.6800%",
    "type": "中英混合"
  },
  {
    "length": "50000byte",
    "encLength": "999392byte",
    "time": "2669ms",
    "ratio": "1998.7840%",
    "type": "中英混合"
  },
  {
    "length": "100000byte",
    "encLength": "1998720byte",
    "time": "4953ms",
    "ratio": "1998.7200%",
    "type": "中英混合"
  }
]

开源协议

MIT License

参与贡献

PR

发包流程

  1. 修改代码并测试通过 npm test
  2. commit 代码 git commit -m "..."
  3. 修改 package.json 版本号
  4. 生成变更记录 npm run changelog
  5. commit package.jsonCHANGELOG.md 文件
  6. 创建 Tag git tag xxx
  7. 推送 Tag git push origin master --tags
  8. 构建项目 npm run build
  9. 发布 npm 包 npm publish

变更记录生成请参考:conventional-changelog

Package Sidebar

Install

npm i decrypt-core

Weekly Downloads

13

Version

1.2.2

License

MIT

Unpacked Size

557 kB

Total Files

76

Last publish

Collaborators

  • johniexu