0.1.2 • Published 3 years ago

third.kit v0.1.2

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

third.kit

third 套件的通用代码集合

1. 安装

npm i third.kit

2. 使用

const third = require("third.kit");

(async () => {
  await third.init("/tmp/");
  console.log(third.runtime.key.fingerprint);
})();

3. 模块/函数

3.1 init(函数)

初始化 kit 组件,必须调用,多次调用此函数则只有第一次会执行

const third = require("third.kit");

(async () => {
  // 参数为设置根目录路径,会自动创建目录
  await third.init("/tmp/11111"); // 设置运行时root路径为'/tmp/11111'文件夹
  await third.init("/tmp/22222"); // 设置运行时root路径为'/tmp/22222'文件夹
  await third.init(); // 设置运行时环境为当前目录
  // stdout: /tmp/11111
  console.log(third.runtime.root);
})();

3.2 pgp(模块)

pgp 加密组件实现

3.2.1 openpgp

基于openpgpjs实现

const third = require("third.kit");
const { openpgp } = third.pgp;

(async () => {
  /** 初始化key */
  // 创建新的key
  const k1 = await openpgp.generate();
  const k2 = await openpgp.generate();
  // 使用已有的key
  const k3 = await openpgp.init(k1.prikey);
  // stdout: true
  console.log(k1.fingerprint === k3.fingerprint);

  /** 签名和验证签名 */
  const message = "hello third";
  // 签名
  const signed_message = await k1.sign(message);
  // 验证签名,如果验证失败则会throw Error,注意处理错误情况
  const v = await openpgp.verify(signed_message, k1.pubkey);
  // created 为以秒为单位的时间戳
  // stdout: true true 1626590564
  console.log(v.text === message, v.fingerprint === k1.fingerprint, v.created);

  /** 加密和解密 */
  // 为k1加密,不签名
  const encrypted_message = await openpgp.encrypt(message, k1.pubkey);
  // 为k1加密并使用k2签名
  const encrypted_signed_message = await k2.encrypt(message, k1.pubkey);
  // 使用k1解密
  const d = await k1.decrypt(encrypted_message);
  // stdout: true
  console.log(d.text === message);
  // 使用k1解密并验证签名
  const ds = await k1.decrypt(encrypted_signed_message, k2.pubkey);
  // created 为以秒为单位的时间戳
  // stdout: true true 1626590564
  console.log(
    ds.text === message,
    ds.fingerprint === k2.fingerprint,
    ds.created
  );
})();

3.2.2 gpg

基于命令行调用 gpg 实现,继承 openpgp,增加了几个 gpg 的方法,以下只演示 gpg 的使用方法

const third = require("third.kit");
const { gpg } = third.pgp;

const key = {
  fingerprint: "44C0F7AF58A8CDBEFD405A1451D98C07AAAAAAAA",
  armored:
    "-----BEGIN PGP PRIVATE KEY BLOCK-----\n\nlFgEYLIYYBYJKwYBBAHaRw8BAQdABwfYq365FaSDGG+CORjkRkcPUbuJ8NgM8K3T\n3I28HYYAAP9sXKZ7FfTsUMMF67oz/UKeH/P7j0VOzlnCAQ2jy+rHshKhtGN0aGly\nZC5jb3JlICh0aGlyZC5jb3JlIHRlc3QgYWNjb3VudCBrZXksICEhIURPIE5PVCBV\nU0UgVEhJUyBLRVkhISEsIGFueW9uZSBjYW4gZ2V0IHRoaXMgc2VjcmV0IGtleSmI\nkwQTFggAOwIbAwULCQgHAgYVCgkICwIEFgIDAQIeAQIXgBYhBETA969YqM2+/UBa\nFFHZjAeqqqqqBQJgtw1tAhkBAAoJEFHZjAeqqqqqZqMBAJ5AHjDEikdSwoTujNc/\njk/nvMnDIPiVtZPOs+A2xm4XAQDVekC5N6rXjzSYw6EupCidiFW4d+xyyAJuTv9N\nz/d2AJxdBGCyOakSCisGAQQBl1UBBQEBB0BRSBCZDWMwL1RN085EpBu/KLXyhD4i\n4M5yTugOGqoCHwMBCAcAAP9Nt1dQCNbX0GjaIdjtAp8yZbALbP1gHimGWF+QFi7e\n+BBBiHgEGBYIAAkFAmC0j+QCGwwAIQkQUdmMB6qqqqoWIQREwPevWKjNvv1AWhRR\n2YwHqqqqqsd1AP9L+gTP+23MUrtclXQN4pmb8WLYXSyy5wDsDcq263OlpAEAyjLU\n8zk0Kpfq5Uhr9AZOh2kFb2mkB0v8oJtWYA6LAQ0=\n=3qgO\n-----END PGP PRIVATE KEY BLOCK-----\n",
};

(async () => {
  // 导入key
  await gpg.import(key.armored);
  // 列出key
  const r = await gpg.list("aaaaaaaa", true);
  console.log(r);
  // 使用key,基于指纹来指定要使用的gpg key
  // k1 的使用方法于openpgp对象一致
  const k1 = await gpg.init(key.fingerprint);
  // 删除key(会删除私钥,谨慎使用)
  await gpg.delete(key.fingerprint);
})();

3.3 runtime(模块)

供 third 其他模块使用的运行时变量

  • 包括运行时变量

    • key 自动加载'~/.third/key'(如果没有则自动创建)
    • root third 其他套件所使用的根目录路径
  • 函数

    • init 初始化函数,无需主动调用,third.kit.init 初始化时会自动调用
const third = require("third.kit");

(async () => {
  // 设置运行时root路径为'/tmp/11111'文件夹
  await third.init("/tmp/11111");
  // stdout:true
  console.log(third.runtime.key instanceof third.pgp.openpgp);
  // stdout:/tmp/11111
  console.log(third.runtime.root);
})();

3.4 aes256

aes256 加密模块

const third = require("third.kit");
const aes256 = third.aes256;

(async () => {
  // 创建一个aes256对象
  const a1 = new aes256();
  // 反序列化:使用已有的数据创建一个相同的aes256对象
  const a2 = new aes256(a1.valueOf());
  const message = "hello third";
  // 加密
  const encrypted = a1.encrypt(message);
  // 解密
  const decrypted = a2.decrypt(encrypted);
  // stdout: true
  console.log(message === decrypted);
})();

3.5 func

常用函数集

3.5.1 sleep

timeout 时间单位为毫秒

3.6 run

3.6.1 once

无论调用多少次,函数只执行一次(类似装饰器使用方法)

const third = require("third.kit");

const run_only_once_func = third.run.once((i) => {
  return i;
});

(async () => {
  // stdout: 0
  console.log(await run_only_once_func(0));
  // stdout: 0
  console.log(await run_only_once_func(1));
})();

todo

  • 增加命令行,返回设备 key
0.1.2

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago

0.0.10

3 years ago

0.0.11

3 years ago

0.0.12

3 years ago

0.0.9

3 years ago

0.0.8

3 years ago

0.0.7

3 years ago

0.0.6

3 years ago

0.0.5

3 years ago

0.0.4

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago