@pagoda-tools/crypto
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

@pagoda-tools/crypto

客户端加解密工具

经测试的浏览器

任何支持文件和文件读取器的浏览器都应该可以工作。以下浏览器已经过测试并正常工作:

  • IE10 (Windows 7)
  • Chrome 42 (OS X 10.10.3)
  • Firefox 38 (OS X 10.10.3)
  • Safari 8.0.6 (OS X 10.10.3)
  • Opera 29.0 (OS X 10.10.3)

请随时更新此列表或通过拉取请求提交特定浏览器的修复程序。

安装

@pagoda-tools/crypto可通过npmyarn获得:

# install npm
npm install @pagoda-tools/crypto -S

# install yarn
yarn add @pagoda-tools/crypto -S

用法

引入

// 本地路径水印
import PdBizDataCrypt from '@pagoda-tools/crypto';

初始化 crypto.js

import axios from 'axios';

const crypto = new PdBizDataCrypt({
  // 客服端 保存的公钥
  public_key: '通过申请得来的公钥',
  // 获得授权
  async getAuthorization({ encryptedData }) {
    if (typeof encryptedData !== 'string') return;
    try {
      // 调用接口获取授权
      const { timestamp, encryptedData: encrypted_data } = axios.post(
        '/getAuthorization',
        {
          encryptedData,
        }
      );
      return {
        timestamp: timestamp,
        encryptedData: encrypted_data,
      };
    } catch (e) {
      console.log('getAuthorization:error');
      throw e;
    }
  },
});

export default crypto;

加密与解密

import crypto from './crypto.js';

const data = { aaa: 123 };

// 数据加密
const { encryptedData, iv } = await crypto.encrypt(data);

// 数据解密
const { aaa: a1 } = await crypto.decrypt(encryptedData, iv);

// a1 === aaa ==> true

Readme

Keywords

Package Sidebar

Install

npm i @pagoda-tools/crypto

Weekly Downloads

0

Version

1.0.1

License

none

Unpacked Size

174 kB

Total Files

10

Last publish

Collaborators

  • xwjweb
  • aaron-zm
  • ckang1229