0.0.2 • Published 2 years ago

@youzanyun-fe/youzanyun-data-security v0.0.2

Weekly downloads
-
License
ISC
Repository
gitlab
Last release
2 years ago

有赞云数据加密 NodeJS 版

npm 依赖安装

npm install @youzanyun-fe/youzanyun-data-security --save

示例

const DataSecurity = require('@youzanyun-fe/youzanyun-data-security');

(async () => {
  const clientId = '68ffaf8440a4babcde';
  const clientSecret = 'b0030740948e368c4ca21ed5d0b123456';
  const kdtId = 789456;
  const ds = new DataSecurity(clientId, clientSecret);

  // 加密
  const cipherText = await ds.encrypt(kdtId, '13012341234');
  const cipherText2 = await ds.encrypt(kdtId, '18812341234');
  // 解密
  const source = await ds.decrypt(kdtId, cipherText); // 13012341234
  // 解密并脱敏
  const decryptMaskResult = await ds.decryptMask(kdtId, cipherText, DataSecurity.MASK_TYPE.MOBILE); // 130****1234

  // 批量加密
  const encryptResult = await ds.batchEncrypt(kdtId, ['13012341234', '浙江省杭州市']); // {"13012341234": 'xxxxx', "浙江省杭州市": xxxxx}
  // 批量解密
  const mobiles = batchDecrypt(kdtId, [cipherText, cipherText2]);
  // 批量解密并脱敏
  const result = await ds.batchDecryptMask(
    kdtId,
    [cipherText, cipherText2],
    SecretClient.MASK_TYPE.BANK_CARD,
  );

  // 生成模糊搜索字符串
  const searchStr = await ds.generateEncryptSearchDigest(kdtId, cipherText);

  // 是否是密文
  const isCipher = DataSecurity.isEncrypt(cipherText); // true
  // 批量判断是否是密文
  const isCiphers = DataSecurity.isEncrypt([cipherText, cipherText2]); // true
})();

API

constructor()

构造器函数

参数说明
字段类型说明
clientIdstringclientId
clientSecretstringclientSecret
示例
const ds = new DataSecurity(clientId, clientSecret);

encrypt(kdtId, source)

加密方法,返回加密后的字符串

参数说明
字段类型说明
kdtIdnumber店铺 id
sourcestring明文
示例
const cipherText = await ds.encrypt(kdtId, '13012341234');
const cipherText2 = await ds.encrypt(kdtId, '18812341234');

batchEncrypt(kdtId, sources)

批量加密方法,返回一个 Object,key 为明文,value 为密文。

参数说明
字段类型说明
kdtIdnumber店铺 id
sourcesstring[]明文数组
示例
const encryptResult = await ds.batchEncrypt(kdtId, ['13012341234', '浙江省杭州市']); // {"13012341234": 'xxxxx', "浙江省杭州市": xxxxx}

decrypt(kdtId, cipherText)

解密方法,返回解密后的明文,传入明文时返回明文。tip: 解密失败会抛异常

参数说明
字段类型说明
kdtIdnumber店铺 id
cipherTextstring密文
示例
// 加密
const cipherText = await ds.encrypt(kdtId, '13012341234');
// 解密
const source = await ds.decrypt(kdtId, cipherText); // 13012341234

batchDecrypt(kdtId, cipherTexts)

批量解密,返回一个 Object,key 为密文,value 为明文。

参数说明
字段类型说明
kdtIdnumber店铺 id
cipherTextsstring[]密文数组
示例
// 加密
const cipherText = await ds.encrypt(kdtId, '13012341234');
const cipherText2 = await ds.encrypt(kdtId, '18812341234');
// 批量解密
const mobiles = batchDecrypt(kdtId, [cipherText, cipherText2]);

decryptMask(kdtId, cipherText, type)

解密并脱敏,返回解密并脱敏后的明文。

参数说明
字段类型说明
kdtIdnumber店铺 id
cipherTextstring密文
typenumber脱敏类型 (参考文档最后的脱敏类型枚举)
示例
// 加密
const cipherText = await ds.encrypt(kdtId, '13012341234');
// 解密并脱敏
const decryptMaskResult = await ds.decryptMask(kdtId, cipherText, DataSecurity.MASK_TYPE.MOBILE); // 130****1234

batchDecryptMask(kdtId, cipherTexts, type)

批量解密并脱敏,返回一个 Object,key 为密文,value 为解密并脱敏后的明文。

参数说明
字段类型说明
kdtIdnumber店铺 id
cipherTextsstring[]密文数组
typenumber脱敏类型 (参考文档最后的脱敏类型枚举)
示例
// 加密
const cipherText = await ds.encrypt(kdtId, '13012341234');
const cipherText2 = await ds.encrypt(kdtId, '18812341234');
// 批量解密并脱敏
const result = await ds.batchDecryptMask(
  kdtId,
  [cipherText, cipherText2],
  SecretClient.MASK_TYPE.BANK_CARD,
);

generateEncryptSearchDigest(kdtId, source)

生成模糊加密字符串, 支持部分搜索,返回一个结果字符串。

参数说明
字段类型说明
kdtIdnumber店铺 id
sourcestring明文
示例
// 加密
const cipherText = await ds.encrypt(kdtId, '13012341234');
// 生成模糊搜索字符串
const searchStr = await ds.generateEncryptSearchDigest(kdtId, cipherText);

static MASK_TYPE

静态属性,脱敏类型枚举定义

类型类型说明
ADDRESS地址0
BANK_CARD银行卡号1
NAME用户名2
EMAIL邮箱地址3
COMPANY_NAME企业名称4
ID_CARD身份证号5
MOBILE手机号码6
示例
// 加密
const cipherText = await ds.encrypt(kdtId, '13012341234');
// 解密并脱敏
const decryptMaskResult = await ds.decryptMask(kdtId, cipherText, DataSecurity.MASK_TYPE.MOBILE); // 130****1234

static isEncrypt(cipherText)

判断输入是不是密文,返回 true/false。

示例
const cipherText = await ds.encrypt(kdtId, '13012341234');
const isCipher = DataSecurity.isEncrypt(cipherText); // true

static batchIsEncrypt(cipherTexts)

批量是否密文判断,返回 true/false。

示例
// 加密
const cipherText = await ds.encrypt(kdtId, '13012341234');
const cipherText2 = await ds.encrypt(kdtId, '18812341234');

const isCipher = DataSecurity.isEncrypt([cipherText, cipherText2]);