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

license-node

v0.0.7

Published

Node.js 生成rsa非对称秘钥对

Downloads

76

Readme

node-license - 使用rsa非对称秘钥对生成license

Node.js 生成rsa非对称秘钥对

  1. 在Node.js v10.12.0引入了generateKeyPair方法, 用于生成公钥与私钥
  2. 我看了generateKeyPair方法Node源码,c++层还是使用的openssl的函数库,所以只是扩展了在Node里面可以直接生成秘钥而已
  3. 如果公钥丢失使用openssl rsa -in private.pem -pubout > public.pem 命令找回公钥,如果私钥丢失或者私钥的口令也忘记了,就彻底找不回信息了
  4. Node.js 暂时没有提供根据私钥找回公钥的方法

rsa非对称秘钥对

对称加密与非对称加密

  1. 对称加密:加密和解密使用的是同一个密钥,加解密双方必须使用同一个密钥才能进行正常的沟通。
  2. 非对称加密:需要两个密钥来进行加密和解密,公开密钥(public key,简称公钥)和私有密钥(private key,简称私钥) ,公钥加密的信息只有私钥才能解开,私钥加密的信息只有公钥才能解开。

需要注意的一点,这个公钥和私钥必须是一对的,如果用公钥对数据进行加密,那么只有使用对应的私钥才能解密,所以只要私钥不泄露,那么我们的数据就是安全的。

非对称加密中,究竟是公钥加密还是私钥加密

  1. 对于加密:公钥加密,私钥加密。毕竟公钥可以公开,但是私钥只有你自已知道,你也同样希望只有你自已才能解密
  2. 对于签名:私钥加密,公钥解密。好比你的签名只有你自已签的才是真的,别人签的都是假的。

Install

# npm i @dtwave/node-license --save

Usage

const License = require('@dtwave/node-license');

const license = new License({
  subject: 'license',
  date: {
    unit: 'years', // days or weeks or months or years
    value: 1
  },
  description: '给产品授权一个一年有效期的license',
  licenseCheckModel: {
    macAddress: ['f0:18:98:32:4e:e5']
  }
});

// 可以不传文件路径, 结果直接返回私钥,公钥。
// 私钥和私钥口令记得保留,作为找回公钥的凭证,私钥丢失就彻底凉凉了
const {publicKey, privateKey} = license.createKeyPairFile('private_waveview', '', '');

// 根据私钥生成license签名
const sign = license.privateEncrypt(privateKey, 'private_waveview');

// 打包公钥和私钥生成的签名
const newLicenseObj = {
  publicKey,
  sign,
}
// 生成打包公钥和签名的base64编码, 用于给到项目部署
const newLicense = Buffer.from(JSON.stringify(newLicenseObj)).toString('base64');
// 转换成json
const result = JSON.parse(Buffer.from(newLicense, 'base64').toString());
// 使用公钥去解密签名
const info = license.publicDecrypt(result.publicKey, Buffer.from(result.sign, 'base64'), '',);

/**
 *
 * 判断mac地址满足
 * @param {String[]} macAddress 客户硬件mac地址
 * @param {String[]} macList 当前机器可用网卡的mac地址
 * @return {Boolean} 是否满足
 */
const isMac = (macAddress, macList) => {
  for (const mac of macAddress) {
    if (macList.includes(mac)) {
      return true;
    }
  }
  return false;
};
if (isMac(info.licenseCheckModel.macAddress, macList)) {
  console.log('可用继续使用产品哦');
  const remainAt = moment(info.expiryAt).diff(moment(), 'days');
  if (remainAt > 0) {
    console.log('剩余时间:', remainAt, '天');
  } else {
    console.log('产品已经到期了,如果想继续使用请联系管理员');
  }
} else {
  console.log('当前服务器的Mac地址没在授权范围内');
}

参考文章

RSA非对称加密算法

Spring Boot项目中使用 TrueLicense 生成和验证License

在非对称加密中,是怎么做到知道公钥也没法破解密文的?

密钥的生成 RSA算法特点与应用注意事项 使用rsa进行http传输加密