rename all libs from xxx.yyy to xxx-yyy

This commit is contained in:
陆柯 2022-06-04 12:08:49 +08:00
parent 24450a4022
commit dff0b62d64
3 changed files with 64 additions and 64 deletions

View File

@ -1,4 +1,4 @@
# tic.crypto # tic-crypto
时光链区块链密码学算法工具库:为区块链相关应用开发提供一套底层的基础算法工具库,用来处理哈希、加解密、签名、助记词、等等。 时光链区块链密码学算法工具库:为区块链相关应用开发提供一套底层的基础算法工具库,用来处理哈希、加解密、签名、助记词、等等。
@ -24,7 +24,7 @@
在前后端软件的 package.json 的依赖清单中引入本库: 在前后端软件的 package.json 的依赖清单中引入本库:
``` ```
npm install git+https://git.faronear.org/npm/tic.crypto#RELEASE_OR_BRANCH --save npm install git+https://git.faronear.org/npm/tic-crypto#RELEASE_OR_BRANCH --save
``` ```
## 用法 ## 用法
@ -32,10 +32,10 @@ npm install git+https://git.faronear.org/npm/tic.crypto#RELEASE_OR_BRANCH --save
基本用法示例: 基本用法示例:
``` ```
let ticCrypto=require('tic.crypto') // 引用 let ticrypto=require('tic-crypto') // 引用
let sw=ticCrypto.randomSecword() // 生成一个随机的助记词(即密语)。或者使用现成的密语。 let sw=ticrypto.randomSecword() // 生成一个随机的助记词(即密语)。或者使用现成的密语。
let kp=ticCrypto.secword2keypair(sw) // 把密语转换成公私钥 let kp=ticrypto.secword2keypair(sw) // 把密语转换成公私钥
let address=ticCrypto.secword2address(sw) // 把密语转换成地址 let address=ticrypto.secword2address(sw) // 把密语转换成地址
``` ```
## 其他 ## 其他
@ -91,9 +91,9 @@ crypto.publicEncrypt(kp.publicKey, Buffer.from('sdafasfdsaf'))
返回 Buffer。每次结果不一样 返回 Buffer。每次结果不一样
似乎 crypto 一定要 rsa 公私钥才可以用加解密ticCrypto.randomKeypair() 生成的 ecc 公私钥不行。 似乎 crypto 一定要 rsa 公私钥才可以用加解密ticrypto.randomKeypair() 生成的 ecc 公私钥不行。
而 eccrypto 和 eccrypto-js 可以用。eccrypto.generateKeyPair() 生成的和 ticCrypto.randomKeypair() 一样 而 eccrypto 和 eccrypto-js 可以用。eccrypto.generateKeyPair() 生成的和 ticrypto.randomKeypair() 一样
eccrypto 在 windows 上的安装有麻烦,一来需要手工安装 OpenSSL 到 c:\openssl-win64\,二来 openssl 1.1.0 起把 libeay32.lib 改名为 libcrypto.dll而 eccrypto 需要 c:\openssl-win64\lib\libeay32.lib会报错 eccrypto 在 windows 上的安装有麻烦,一来需要手工安装 OpenSSL 到 c:\openssl-win64\,二来 openssl 1.1.0 起把 libeay32.lib 改名为 libcrypto.dll而 eccrypto 需要 c:\openssl-win64\lib\libeay32.lib会报错

110
index.js
View File

@ -45,9 +45,9 @@ my.REGEXP_ALPHABET = {
/** /**
* *
* @class TICrypto * @class Ticrypto
*/ */
class TICrypto { class Ticrypto {
/** /**
* 测试输入数据是否可哈希混淆 * 测试输入数据是否可哈希混淆
* *
@ -55,7 +55,7 @@ class TICrypto {
* @param {*} data 需要被哈希混淆的数据 * @param {*} data 需要被哈希混淆的数据
* @param {*} option 可选参数 * @param {*} option 可选参数
* @return {Boolean} * @return {Boolean}
* @memberof TICrypto * @memberof Ticrypto
*/ */
static isHashable (data, { strict = false } = {}) { static isHashable (data, { strict = false } = {}) {
if (strict) { if (strict) {
@ -71,7 +71,7 @@ class TICrypto {
* @param {String} hash * @param {String} hash
* @param {Object} option [{ hasher = my.HASHER }={}] * @param {Object} option [{ hasher = my.HASHER }={}]
* @return {Boolean} * @return {Boolean}
* @memberof TICrypto * @memberof Ticrypto
*/ */
static isHash (hash, { hasher = my.HASHER } = {}) { static isHash (hash, { hasher = my.HASHER } = {}) {
if (my.HASHER_LIST.includes(hasher)) { if (my.HASHER_LIST.includes(hasher)) {
@ -97,7 +97,7 @@ class TICrypto {
* @param {String} secword * @param {String} secword
* @param {Object} option [{ mode = 'strict' }={}] * @param {Object} option [{ mode = 'strict' }={}]
* @return {Boolean} * @return {Boolean}
* @memberof TICrypto * @memberof Ticrypto
*/ */
static isSecword (secword, { mode = 'strict' } = {}) { static isSecword (secword, { mode = 'strict' } = {}) {
// 注意 not all 12 words combinations are valid for both bitcore and bip39, because there are checksum in mnemonic. 另外实际上bitcore和bip39对12, 15, 18, ... 长度的合法助记词都返回 true。 // 注意 not all 12 words combinations are valid for both bitcore and bip39, because there are checksum in mnemonic. 另外实际上bitcore和bip39对12, 15, 18, ... 长度的合法助记词都返回 true。
@ -124,7 +124,7 @@ class TICrypto {
* @static * @static
* @param {String} seckey * @param {String} seckey
* @return {Boolean} * @return {Boolean}
* @memberof TICrypto * @memberof Ticrypto
*/ */
static isSeckey (seckey) { static isSeckey (seckey) {
// 比特币、以太坊的私钥64 hex // 比特币、以太坊的私钥64 hex
@ -138,7 +138,7 @@ class TICrypto {
* @static * @static
* @param {String} pubkey * @param {String} pubkey
* @return {Boolean} * @return {Boolean}
* @memberof TICrypto * @memberof Ticrypto
*/ */
static isPubkey (pubkey) { static isPubkey (pubkey) {
// 比特币的公钥:压缩型 '02|03' + 64 hex 或 无压缩型 '04' + 128 hex // 比特币的公钥:压缩型 '02|03' + 64 hex 或 无压缩型 '04' + 128 hex
@ -153,7 +153,7 @@ class TICrypto {
* @static * @static
* @param {String} signature * @param {String} signature
* @return {Boolean} * @return {Boolean}
* @memberof TICrypto * @memberof Ticrypto
*/ */
static isSignature (signature) { static isSignature (signature) {
return /^[a-fA-F0-9]{128,144}$/.test(signature) && signature.length % 2 === 0 // 128 for nacl, 140/142/144 for crypto and eccrypto in der format. return /^[a-fA-F0-9]{128,144}$/.test(signature) && signature.length % 2 === 0 // 128 for nacl, 140/142/144 for crypto and eccrypto in der format.
@ -166,7 +166,7 @@ class TICrypto {
* @param {*} data * @param {*} data
* @param {option} [{ hasher = my.HASHER, salt, input = my.INPUT, output = my.OUTPUT }={}] * @param {option} [{ hasher = my.HASHER, salt, input = my.INPUT, output = my.OUTPUT }={}]
* @return {String} * @return {String}
* @memberof TICrypto * @memberof Ticrypto
*/ */
static hash (data, { hasher = my.HASHER, salt, input = my.INPUT, output = my.OUTPUT } = {}) { static hash (data, { hasher = my.HASHER, salt, input = my.INPUT, output = my.OUTPUT } = {}) {
// data can be anything, but converts to string or remains be Buffer/TypedArray/DataView // data can be anything, but converts to string or remains be Buffer/TypedArray/DataView
@ -190,7 +190,7 @@ class TICrypto {
* @param {*} data * @param {*} data
* @param {*} option [{ tool, keytype, key, input, output, cipher }={}] * @param {*} option [{ tool, keytype, key, input, output, cipher }={}]
* @return {String} * @return {String}
* @memberof TICrypto * @memberof Ticrypto
*/ */
static async encrypt ({ data, tool = 'crypto', keytype = 'pwd', key, input, output, cipher } = {}) { static async encrypt ({ data, tool = 'crypto', keytype = 'pwd', key, input, output, cipher } = {}) {
if (tool === 'eccrypto') { if (tool === 'eccrypto') {
@ -213,7 +213,7 @@ class TICrypto {
return { iv: iv.toString('hex'), ciphertext } // 有 iv显然每次结果不一样 return { iv: iv.toString('hex'), ciphertext } // 有 iv显然每次结果不一样
} }
} else if (keytype === 'seckey') { } else if (keytype === 'seckey') {
// 尚未走通,不能使用 ticCrypto 生成的 Elliptic curve 椭圆曲线算法公私钥,只能用 crypto.generateKeypairs() 生成的 rsa 公私钥 // 尚未走通,不能使用 ticrypto 生成的 Elliptic curve 椭圆曲线算法公私钥,只能用 crypto.generateKeypairs() 生成的 rsa 公私钥
let seckeyPEM = await new keyman.Key('oct', this.hex_to_buf(key), { namedCurve: 'P-256K' }).export('pem') // 私钥导出的der格式为144字节。 let seckeyPEM = await new keyman.Key('oct', this.hex_to_buf(key), { namedCurve: 'P-256K' }).export('pem') // 私钥导出的der格式为144字节。
return crypto.privateEncrypt(seckeyPEM, Buffer.from(data)) // 返回 Buffer。每次结果都一样。 return crypto.privateEncrypt(seckeyPEM, Buffer.from(data)) // 返回 Buffer。每次结果都一样。
} else if (keytype === 'pubkey') { } else if (keytype === 'pubkey') {
@ -230,7 +230,7 @@ class TICrypto {
* @param {*} data * @param {*} data
* @param {Object} option [{ keytype, key, input, output, cipher, format }={}] * @param {Object} option [{ keytype, key, input, output, cipher, format }={}]
* @return {String} * @return {String}
* @memberof TICrypto * @memberof Ticrypto
*/ */
static async decrypt ({ data = {}, tool = 'crypto', keytype = 'pwd', key, input, output, cipher } = {}) { static async decrypt ({ data = {}, tool = 'crypto', keytype = 'pwd', key, input, output, cipher } = {}) {
// data 应当是 encrypt 输出的数据类型 // data 应当是 encrypt 输出的数据类型
@ -261,7 +261,7 @@ class TICrypto {
return decrypted return decrypted
} }
} else if (keytype === 'seckey') { } else if (keytype === 'seckey') {
// 尚未走通,不能使用 ticCrypto 生成的 Elliptic curve 椭圆曲线算法公私钥 // 尚未走通,不能使用 ticrypto 生成的 Elliptic curve 椭圆曲线算法公私钥
let seckeyPEM = await new keyman.Key('oct', this.hex_to_buf(key), { namedCurve: 'P-256K' }).export('pem') // 私钥导出的der格式为144字节。 let seckeyPEM = await new keyman.Key('oct', this.hex_to_buf(key), { namedCurve: 'P-256K' }).export('pem') // 私钥导出的der格式为144字节。
return crypto.privateDecrypt(seckeyPEM, Buffer.from(data)) // 返回 Buffer。每次结果都一样。 return crypto.privateDecrypt(seckeyPEM, Buffer.from(data)) // 返回 Buffer。每次结果都一样。
} else if (keytype === 'pubkey') { } else if (keytype === 'pubkey') {
@ -279,7 +279,7 @@ class TICrypto {
* @param {String} seckey * @param {String} seckey
* @param {Object} option [option={}] * @param {Object} option [option={}]
* @return {String} * @return {String}
* @memberof TICrypto * @memberof Ticrypto
*/ */
static async sign ({ data, seckey, tool = 'crypto', hasher }) { static async sign ({ data, seckey, tool = 'crypto', hasher }) {
// data can be string or buffer or object, results are the same // data can be string or buffer or object, results are the same
@ -316,7 +316,7 @@ class TICrypto {
* @param {String} pubkey * @param {String} pubkey
* @param {Object} option [option={}] * @param {Object} option [option={}]
* @return {Boolean} * @return {Boolean}
* @memberof TICrypto * @memberof Ticrypto
*/ */
static async verify ({ data, signature, pubkey, tool = 'crypto', hasher }) { static async verify ({ data, signature, pubkey, tool = 'crypto', hasher }) {
// data could be anything, but converts to string or remains be Buffer/TypedArray/DataView // data could be anything, but converts to string or remains be Buffer/TypedArray/DataView
@ -357,7 +357,7 @@ class TICrypto {
* @param {String} pass * @param {String} pass
* @param {Object} option * @param {Object} option
* @return {Object} {pubkey, seckey, address,} * @return {Object} {pubkey, seckey, address,}
* @memberof TICrypto * @memberof Ticrypto
*/ */
static pass2keypair (pass, { hasher } = {}) { static pass2keypair (pass, { hasher } = {}) {
// 如果使用其他机制例如密码、随机数不使用secword也可生成keypair // 如果使用其他机制例如密码、随机数不使用secword也可生成keypair
@ -383,7 +383,7 @@ class TICrypto {
* @static * @static
* @param {*} entropy * @param {*} entropy
* @return {String} * @return {String}
* @memberof TICrypto * @memberof Ticrypto
*/ */
static entropy2secword (entropy) { static entropy2secword (entropy) {
// entropy could be hex string or buffer. Byte length could be of 16, 20, 24, 28, ... which outputs mnemonic of length 12, 15, 18, 21, ... // entropy could be hex string or buffer. Byte length could be of 16, 20, 24, 28, ... which outputs mnemonic of length 12, 15, 18, 21, ...
@ -396,7 +396,7 @@ class TICrypto {
* @static * @static
* @param {String} secword * @param {String} secword
* @return {*} * @return {*}
* @memberof TICrypto * @memberof Ticrypto
*/ */
static secword2entropy (secword) { static secword2entropy (secword) {
// secword could be of length 12, 15, 18, ... which outputs hex of length 32, 40, ... // secword could be of length 12, 15, 18, ... which outputs hex of length 32, 40, ...
@ -410,7 +410,7 @@ class TICrypto {
* @param {String} secword * @param {String} secword
* @param {Object} option * @param {Object} option
* @return {Object} {pubkey, seckey,} * @return {Object} {pubkey, seckey,}
* @memberof TICrypto * @memberof Ticrypto
*/ */
static secword2keypair (secword, { coin, pass, path, tool, hasher } = {}) { static secword2keypair (secword, { coin, pass, path, tool, hasher } = {}) {
// coin 币种; // coin 币种;
@ -471,7 +471,7 @@ class TICrypto {
* @param {*} seed * @param {*} seed
* @param {string} option [{ coin = my.COIN }={ coin: my.COIN }] * @param {string} option [{ coin = my.COIN }={ coin: my.COIN }]
* @return {String} path * @return {String} path
* @memberof TICrypto * @memberof Ticrypto
*/ */
static seed2path ({ seed, coin = my.COIN } = { coin: my.COIN }) { static seed2path ({ seed, coin = my.COIN } = { coin: my.COIN }) {
// 路径规范 BIP44: m/Purpose'/Coin'/Account'/Change/Index, // 路径规范 BIP44: m/Purpose'/Coin'/Account'/Change/Index,
@ -526,7 +526,7 @@ class TICrypto {
* @param {String} secword * @param {String} secword
* @param {Object} option * @param {Object} option
* @return {Object} * @return {Object}
* @memberof TICrypto * @memberof Ticrypto
*/ */
static secword2account (secword, { coin, pass, path, tool, hasher } = {}) { static secword2account (secword, { coin, pass, path, tool, hasher } = {}) {
// account 比 keypair 多了 address 字段。 // account 比 keypair 多了 address 字段。
@ -551,7 +551,7 @@ class TICrypto {
* @param {String} secword * @param {String} secword
* @param {Object} option * @param {Object} option
* @return {String} address * @return {String} address
* @memberof TICrypto * @memberof Ticrypto
*/ */
static secword2address (secword, { coin, world, pass, path, tool, hasher } = {}) { static secword2address (secword, { coin, world, pass, path, tool, hasher } = {}) {
coin = coin?.toUpperCase() || my.COIN coin = coin?.toUpperCase() || my.COIN
@ -575,7 +575,7 @@ class TICrypto {
* @param {*} seckey * @param {*} seckey
* @param {*} [option={}] * @param {*} [option={}]
* @return {*} * @return {*}
* @memberof TICrypto * @memberof Ticrypto
*/ */
static seckey2pubkey (seckey, { curve, compress } = {}) { static seckey2pubkey (seckey, { curve, compress } = {}) {
if (this.isSeckey(seckey) && seckey.length === 64) { if (this.isSeckey(seckey) && seckey.length === 64) {
@ -607,7 +607,7 @@ class TICrypto {
* @param {*} seckey * @param {*} seckey
* @param {*} option * @param {*} option
* @return {*} * @return {*}
* @memberof TICrypto * @memberof Ticrypto
*/ */
static seckey2address (seckey, { coin, world } = {}) { static seckey2address (seckey, { coin, world } = {}) {
coin = coin?.toUpperCase() || my.COIN coin = coin?.toUpperCase() || my.COIN
@ -632,7 +632,7 @@ class TICrypto {
* @param {*} pubkey * @param {*} pubkey
* @param {*} [{ coin }={}] * @param {*} [{ coin }={}]
* @return {*} * @return {*}
* @memberof TICrypto * @memberof Ticrypto
* position 就是通常所说的 PubKeyHash出现在比特币交易的锁定脚本里 * position 就是通常所说的 PubKeyHash出现在比特币交易的锁定脚本里
*/ */
static pubkey2position (pubkey, { coin } = {}) { static pubkey2position (pubkey, { coin } = {}) {
@ -670,7 +670,7 @@ class TICrypto {
* @param {*} position * @param {*} position
* @param {*} [{ coin, world }={}] * @param {*} [{ coin, world }={}]
* @return {*} * @return {*}
* @memberof TICrypto * @memberof Ticrypto
*/ */
static position2address (position, { coin, world } = {}) { static position2address (position, { coin, world } = {}) {
if (!/^[\da-fA-F]{40}$/.test(position)) return null // 不论 tic, btc, eth其 position 都是 40字符的。 if (!/^[\da-fA-F]{40}$/.test(position)) return null // 不论 tic, btc, eth其 position 都是 40字符的。
@ -750,7 +750,7 @@ class TICrypto {
* *
* @static * @static
* @return {*} * @return {*}
* @memberof TICrypto * @memberof Ticrypto
* 地址和PubKeyHash(即position)之间能互相转化 * 地址和PubKeyHash(即position)之间能互相转化
*/ */
static address2position () { static address2position () {
@ -782,7 +782,7 @@ class TICrypto {
* @static * @static
* @param {String} address * @param {String} address
* @return {Boolean} * @return {Boolean}
* @memberof TICrypto * @memberof Ticrypto
*/ */
static isAddress (address) { static isAddress (address) {
if (/^(0x)?[\da-fA-F]{40}$/.test(address)) { if (/^(0x)?[\da-fA-F]{40}$/.test(address)) {
@ -811,7 +811,7 @@ class TICrypto {
* @param {*} pubkey * @param {*} pubkey
* @param {*} [option={}] * @param {*} [option={}]
* @return {*} * @return {*}
* @memberof TICrypto * @memberof Ticrypto
*/ */
static pubkey2address (pubkey, { coin, world } = {}) { static pubkey2address (pubkey, { coin, world } = {}) {
// pubkey 应当是string类型 // pubkey 应当是string类型
@ -826,7 +826,7 @@ class TICrypto {
* @param {*} secword * @param {*} secword
* @param {*} pass * @param {*} pass
* @return {*} * @return {*}
* @memberof TICrypto * @memberof Ticrypto
*/ */
static secword2seed (secword, pass) { static secword2seed (secword, pass) {
// 遵循bip39的算法。和 ether.HDNode.mnemonic2Seed 结果一样是64字节的种子。 // 遵循bip39的算法。和 ether.HDNode.mnemonic2Seed 结果一样是64字节的种子。
@ -840,7 +840,7 @@ class TICrypto {
* @static * @static
* @param {string} [lang='english'] * @param {string} [lang='english']
* @return {*} * @return {*}
* @memberof TICrypto * @memberof Ticrypto
*/ */
static randomSecword (lang = 'english') { static randomSecword (lang = 'english') {
// accepts case-insensitive lang, such as 'chinese, cn, tw, en' // accepts case-insensitive lang, such as 'chinese, cn, tw, en'
@ -884,7 +884,7 @@ class TICrypto {
* @static * @static
* @param {*} [option={}] * @param {*} [option={}]
* @return {*} * @return {*}
* @memberof TICrypto * @memberof Ticrypto
*/ */
static randomSeckey ({ coin, tool } = {}) { static randomSeckey ({ coin, tool } = {}) {
// 跳过 secword 直接产生随机密钥 // 跳过 secword 直接产生随机密钥
@ -901,7 +901,7 @@ class TICrypto {
* @static * @static
* @param {*} [option={}] * @param {*} [option={}]
* @return {*} * @return {*}
* @memberof TICrypto * @memberof Ticrypto
*/ */
static randomKeypair ({ tool, purpose } = {}) { static randomKeypair ({ tool, purpose } = {}) {
let kp let kp
@ -931,7 +931,7 @@ class TICrypto {
* @static * @static
* @param {*} [option={}] * @param {*} [option={}]
* @return {*} * @return {*}
* @memberof TICrypto * @memberof Ticrypto
*/ */
static randomAccount ({ lang, coin, pass, path, tool, hasher } = {}) { static randomAccount ({ lang, coin, pass, path, tool, hasher } = {}) {
let secword = this.randomSecword(lang) let secword = this.randomSecword(lang)
@ -945,7 +945,7 @@ class TICrypto {
* @param {number} [length=6] * @param {number} [length=6]
* @param {*} alphabet * @param {*} alphabet
* @return {*} * @return {*}
* @memberof TICrypto * @memberof Ticrypto
*/ */
static randomString (length = 6, alphabet) { static randomString (length = 6, alphabet) {
// 长度为 length字母表为 alphabet 的随机字符串 // 长度为 length字母表为 alphabet 的随机字符串
@ -963,7 +963,7 @@ class TICrypto {
* @static * @static
* @param {*} [{ length, min, max }={}] * @param {*} [{ length, min, max }={}]
* @return {*} * @return {*}
* @memberof TICrypto * @memberof Ticrypto
*/ */
static randomNumber ({ length, min, max } = {}) { static randomNumber ({ length, min, max } = {}) {
// 长度为 length 的随机数字,或者 (min||0) <= num < max // 长度为 length 的随机数字,或者 (min||0) <= num < max
@ -989,7 +989,7 @@ class TICrypto {
* @param {*} targetLength * @param {*} targetLength
* @param {*} symbol * @param {*} symbol
* @return {*} * @return {*}
* @memberof TICrypto * @memberof Ticrypto
*/ */
static padStart (string, targetLength, symbol) { static padStart (string, targetLength, symbol) {
// 2020-03: 发现在浏览器里,还不支持 string.padStart(),只好自己写个暂代。 // 2020-03: 发现在浏览器里,还不支持 string.padStart(),只好自己写个暂代。
@ -1004,7 +1004,7 @@ class TICrypto {
* 生成 uuid * 生成 uuid
* *
* @static * @static
* @memberof TICrypto * @memberof Ticrypto
*/ */
static randomUuid () { static randomUuid () {
return uuid.v4() return uuid.v4()
@ -1017,7 +1017,7 @@ class TICrypto {
* @param {*} hashList * @param {*} hashList
* @param {*} [option={}] * @param {*} [option={}]
* @return {*} * @return {*}
* @memberof TICrypto * @memberof Ticrypto
*/ */
static getMerkleHash (hashList, { output, hasher } = {}) { static getMerkleHash (hashList, { output, hasher } = {}) {
// merkle算法略有难度暂时用最简单的hash代替 // merkle算法略有难度暂时用最简单的hash代替
@ -1038,7 +1038,7 @@ class TICrypto {
* @param {*} todoHashList * @param {*} todoHashList
* @param {*} option * @param {*} option
* @return {*} * @return {*}
* @memberof TICrypto * @memberof Ticrypto
*/ */
static getMerkleRoot (todoHashList) { static getMerkleRoot (todoHashList) {
//深拷贝传入数组,防止引用对象被改变 //深拷贝传入数组,防止引用对象被改变
@ -1075,7 +1075,7 @@ class TICrypto {
* @param {*} hash * @param {*} hash
* @param {*} sig * @param {*} sig
* @return {*} * @return {*}
* @memberof TICrypto * @memberof Ticrypto
*/ */
static distanceSig (hash, sig) { static distanceSig (hash, sig) {
// hash为64hex字符sig为128hex字符。返回用hex表达的距离。 // hash为64hex字符sig为128hex字符。返回用hex表达的距离。
@ -1097,7 +1097,7 @@ class TICrypto {
* @param {*} sig1 * @param {*} sig1
* @param {*} sig2 * @param {*} sig2
* @return {*} * @return {*}
* @memberof TICrypto * @memberof Ticrypto
*/ */
static compareSig (hash, sig1, sig2) { static compareSig (hash, sig1, sig2) {
// 返回距离hash更近的sig // 返回距离hash更近的sig
@ -1130,7 +1130,7 @@ class TICrypto {
* @param {*} hash * @param {*} hash
* @param {*} sigList * @param {*} sigList
* @return {*} * @return {*}
* @memberof TICrypto * @memberof Ticrypto
*/ */
static sortSigList (hash, sigList) { static sortSigList (hash, sigList) {
if (Array.isArray(sigList) && this.isHash(hash)) { if (Array.isArray(sigList) && this.isHash(hash)) {
@ -1189,7 +1189,7 @@ class TICrypto {
* @param {*} prikey * @param {*} prikey
* @param {*} signType * @param {*} signType
* @return {*} * @return {*}
* @memberof TICrypto * @memberof Ticrypto
*/ */
static rsaSign (string2Sign, prikey, signType) { static rsaSign (string2Sign, prikey, signType) {
signType = signType || 'RSA-SHA1' // could be RSA-SHA256, RSA-SHA1 or more signType = signType || 'RSA-SHA1' // could be RSA-SHA256, RSA-SHA1 or more
@ -1206,7 +1206,7 @@ class TICrypto {
* @param {*} pubkey * @param {*} pubkey
* @param {*} signType * @param {*} signType
* @return {*} * @return {*}
* @memberof TICrypto * @memberof Ticrypto
*/ */
static rsaVerify (string2Verify, signature, pubkey, signType) { static rsaVerify (string2Verify, signature, pubkey, signType) {
signType = signType || 'RSA-SHA1' // could be RSA-SHA256, RSA-SHA1 or more signType = signType || 'RSA-SHA1' // could be RSA-SHA256, RSA-SHA1 or more
@ -1220,7 +1220,7 @@ class TICrypto {
* @static * @static
* @param {*} buffer * @param {*} buffer
* @return {*} * @return {*}
* @memberof TICrypto * @memberof Ticrypto
*/ */
static buf_to_hex (buffer) { static buf_to_hex (buffer) {
// buffer is an ArrayBuffer // buffer is an ArrayBuffer
@ -1233,7 +1233,7 @@ class TICrypto {
* @static * @static
* @param {*} hex * @param {*} hex
* @return {*} * @return {*}
* @memberof TICrypto * @memberof Ticrypto
*/ */
static hex_to_buf (hex) { static hex_to_buf (hex) {
return new Uint8Array( return new Uint8Array(
@ -1249,7 +1249,7 @@ class TICrypto {
* @static * @static
* @param {*} hex * @param {*} hex
* @return {*} * @return {*}
* @memberof TICrypto * @memberof Ticrypto
* 如果出现非HEX的字符从这个字符及其同Byte的另一个字符起直到末尾都会被忽略掉但仍然成功返回一个串 * 如果出现非HEX的字符从这个字符及其同Byte的另一个字符起直到末尾都会被忽略掉但仍然成功返回一个串
* bs58check bs58 可接受string, Buffer, ArrayBuffer, Array 包括空字符串'', 各种内容的数组例如包含 undefined{...}等等; * bs58check bs58 可接受string, Buffer, ArrayBuffer, Array 包括空字符串'', 各种内容的数组例如包含 undefined{...}等等;
* 不可接受 undefined, null, {...}, 等等会返回 exception * 不可接受 undefined, null, {...}, 等等会返回 exception
@ -1276,7 +1276,7 @@ class TICrypto {
* @static * @static
* @param {*} box * @param {*} box
* @return {*} * @return {*}
* @memberof TICrypto * @memberof Ticrypto
*/ */
static b58c_to_hex (box) { static b58c_to_hex (box) {
try { try {
@ -1319,7 +1319,7 @@ class TICrypto {
* @static * @static
* @param {*} hex * @param {*} hex
* @return {*} * @return {*}
* @memberof TICrypto * @memberof Ticrypto
*/ */
static hex_to_b64t (hex) { static hex_to_b64t (hex) {
if (/^[0-9a-fA-F]+$/.test(hex)) { if (/^[0-9a-fA-F]+$/.test(hex)) {
@ -1334,7 +1334,7 @@ class TICrypto {
* @static * @static
* @param {*} b64t * @param {*} b64t
* @return {*} * @return {*}
* @memberof TICrypto * @memberof Ticrypto
*/ */
static b64t_to_hex (b64t) { static b64t_to_hex (b64t) {
if (/^[0-9a-zA-Z\._]+$/.test(b64t)) { if (/^[0-9a-zA-Z\._]+$/.test(b64t)) {
@ -1375,7 +1375,7 @@ class TICrypto {
* @static * @static
* @param {*} hex * @param {*} hex
* @return {*} * @return {*}
* @memberof TICrypto * @memberof Ticrypto
*/ */
static hex_to_eip55 (hex) { static hex_to_eip55 (hex) {
if (/^(0x)?[\da-fA-F]+$/.test(hex)) { if (/^(0x)?[\da-fA-F]+$/.test(hex)) {
@ -1402,7 +1402,7 @@ class TICrypto {
* @static * @static
* @param {*} uncompressed * @param {*} uncompressed
* @return {*} * @return {*}
* @memberof TICrypto * @memberof Ticrypto
*/ */
static compressPubkey (uncompressed) { static compressPubkey (uncompressed) {
// test: https://iancoleman.io/bitcoin-key-compression/ // test: https://iancoleman.io/bitcoin-key-compression/
@ -1427,7 +1427,7 @@ class TICrypto {
* @static * @static
* @param {*} compressed * @param {*} compressed
* @return {*} * @return {*}
* @memberof TICrypto * @memberof Ticrypto
*/ */
static decompressPubkey (compressed) { static decompressPubkey (compressed) {
// uncompress: https://stackoverflow.com/questions/17171542/algorithm-for-elliptic-curve-point-compression/53478265#53478265 // uncompress: https://stackoverflow.com/questions/17171542/algorithm-for-elliptic-curve-point-compression/53478265#53478265
@ -1512,4 +1512,4 @@ class TICrypto {
} }
// 必须单独写 module.exports不要和类定义写在一起否则会导致 jsdoc 解析不到类内文档。 // 必须单独写 module.exports不要和类定义写在一起否则会导致 jsdoc 解析不到类内文档。
module.exports = TICrypto module.exports = Ticrypto

View File

@ -1,5 +1,5 @@
{ {
"name": "tic.crypto", "name": "tic-crypto",
"version": "0.1.0", "version": "0.1.0",
"private": true, "private": true,
"dependencies": { "dependencies": {