rename this.hash to this.hash_easy
This commit is contained in:
parent
cc01e71f48
commit
0cb2547d17
42
ticc.js
42
ticc.js
@ -188,7 +188,7 @@ class TicCrypto {
|
|||||||
// 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
|
||||||
if (this.is_hashable({ data })) {
|
if (this.is_hashable({ data })) {
|
||||||
if (typeof data !== 'string' && !(data instanceof Buffer) && !(data instanceof DataView)) data = JSON.stringify(data)
|
if (typeof data !== 'string' && !(data instanceof Buffer) && !(data instanceof DataView)) data = JSON.stringify(data)
|
||||||
if (salt && typeof salt === 'string') data = data + this.hash(salt)
|
if (salt && typeof salt === 'string') data = data + this.hash_easy(salt)
|
||||||
let inputEncoding = input // my.INPUT_LIST.includes(input)?input:my.INPUT // 'utf8', 'ascii' or 'latin1' for string data, default to utf8 if not specified; ignored for Buffer, TypedArray, or DataView.
|
let inputEncoding = input // my.INPUT_LIST.includes(input)?input:my.INPUT // 'utf8', 'ascii' or 'latin1' for string data, default to utf8 if not specified; ignored for Buffer, TypedArray, or DataView.
|
||||||
let outputEncoding = output === 'buf' ? undefined : output // (my.OUTPUT_LIST.includes(output)?output:my.OUTPUT) // output: 留空=》默认输出hex格式;或者手动指定 'buf', hex', 'latin1' or 'base64'
|
let outputEncoding = output === 'buf' ? undefined : output // (my.OUTPUT_LIST.includes(output)?output:my.OUTPUT) // output: 留空=》默认输出hex格式;或者手动指定 'buf', hex', 'latin1' or 'base64'
|
||||||
return crypto
|
return crypto
|
||||||
@ -222,7 +222,7 @@ class TicCrypto {
|
|||||||
let inputEncoding = my.INPUT_LIST.includes(input) ? input : my.INPUT // 'utf8' by default, 'ascii', 'latin1' for string or ignored for Buffer/TypedArray/DataView
|
let inputEncoding = my.INPUT_LIST.includes(input) ? input : my.INPUT // 'utf8' by default, 'ascii', 'latin1' for string or ignored for Buffer/TypedArray/DataView
|
||||||
let outputEncoding = output === 'buf' ? undefined : my.OUTPUT_LIST.includes(output) ? output : my.OUTPUT // 'latin1', 'base64', 'hex' by default or 'buf' to Buffer explicitly
|
let outputEncoding = output === 'buf' ? undefined : my.OUTPUT_LIST.includes(output) ? output : my.OUTPUT // 'latin1', 'base64', 'hex' by default or 'buf' to Buffer explicitly
|
||||||
const iv = crypto.randomBytes(16)
|
const iv = crypto.randomBytes(16)
|
||||||
let encryptor = crypto.createCipheriv(my.CIPHER_LIST.includes(cipher) ? cipher : my.CIPHER, this.hex_to_buf(this.hash(key)), iv) // cipher 和 key 的长度必须相同,例如 cipher 是 ***-192,那么 key 就必须是 192/8=24 字节 = 48 hex 的。
|
let encryptor = crypto.createCipheriv(my.CIPHER_LIST.includes(cipher) ? cipher : my.CIPHER, this.hex_to_buf(this.hash_easy(key)), iv) // cipher 和 key 的长度必须相同,例如 cipher 是 ***-192,那么 key 就必须是 192/8=24 字节 = 48 hex 的。
|
||||||
if (typeof data !== 'string' && !(data instanceof Buffer) && !(data instanceof DataView)) data = JSON.stringify(data)
|
if (typeof data !== 'string' && !(data instanceof Buffer) && !(data instanceof DataView)) data = JSON.stringify(data)
|
||||||
let ciphertext = encryptor.update(data, inputEncoding, outputEncoding)
|
let ciphertext = encryptor.update(data, inputEncoding, outputEncoding)
|
||||||
ciphertext += encryptor.final(outputEncoding) // 但是 Buffer + Buffer 还是会变成string
|
ciphertext += encryptor.final(outputEncoding) // 但是 Buffer + Buffer 还是会变成string
|
||||||
@ -268,7 +268,7 @@ class TicCrypto {
|
|||||||
let outputEncoding = output === 'buf' ? undefined : my.INPUT_LIST.includes(output) ? output : my.INPUT // output (=input of encrypt) could be 'latin1', 'ascii', 'utf8' by default or 'buf' to Buffer explicitly
|
let outputEncoding = output === 'buf' ? undefined : my.INPUT_LIST.includes(output) ? output : my.INPUT // output (=input of encrypt) could be 'latin1', 'ascii', 'utf8' by default or 'buf' to Buffer explicitly
|
||||||
let decryptor = crypto.createDecipheriv(
|
let decryptor = crypto.createDecipheriv(
|
||||||
my.CIPHER_LIST.includes(cipher) ? cipher : my.CIPHER,
|
my.CIPHER_LIST.includes(cipher) ? cipher : my.CIPHER,
|
||||||
this.hex_to_buf(this.hash(key)),
|
this.hex_to_buf(this.hash_easy(key)),
|
||||||
Buffer.from(data.iv, 'hex')
|
Buffer.from(data.iv, 'hex')
|
||||||
)
|
)
|
||||||
let decrypted = decryptor.update(data.ciphertext, inputEncoding, outputEncoding)
|
let decrypted = decryptor.update(data.ciphertext, inputEncoding, outputEncoding)
|
||||||
@ -302,21 +302,21 @@ class TicCrypto {
|
|||||||
if (this.is_hashable({ data }) && this.is_seckey(seckey)) {
|
if (this.is_hashable({ data }) && this.is_seckey(seckey)) {
|
||||||
if (tool === 'nacl' && seckey.length === 128) {
|
if (tool === 'nacl' && seckey.length === 128) {
|
||||||
// 使用nacl的签名算法。注意,nacl.sign需要的seckey是64字节=128字符。
|
// 使用nacl的签名算法。注意,nacl.sign需要的seckey是64字节=128字符。
|
||||||
let hashBuf = this.hash(data, { output: 'buf' }) // 哈希必须输出为 buffer
|
let hashBuf = this.hash_easy(data, { output: 'buf' }) // 哈希必须输出为 buffer
|
||||||
let signature = nacl.sign.detached(hashBuf, Buffer.from(seckey, 'hex'))
|
let signature = nacl.sign.detached(hashBuf, Buffer.from(seckey, 'hex'))
|
||||||
return Buffer.from(signature).toString('hex') // 签名是64节,128个hex字符
|
return Buffer.from(signature).toString('hex') // 签名是64节,128个hex字符
|
||||||
} else if (tool === 'eccrypto' && seckey.length === 64) {
|
} else if (tool === 'eccrypto' && seckey.length === 64) {
|
||||||
// eccrypto 对同一组data,seckey生成的签名是固定的,观察到hex长度为140或142,是der格式。
|
// eccrypto 对同一组data,seckey生成的签名是固定的,观察到hex长度为140或142,是der格式。
|
||||||
let signature = await eccrypto.sign(Buffer.from(seckey, 'hex'), this.hash(data, { output: 'buf' }))
|
let signature = await eccrypto.sign(Buffer.from(seckey, 'hex'), this.hash_easy(data, { output: 'buf' }))
|
||||||
return signature.toString('hex')
|
return signature.toString('hex')
|
||||||
} else if (seckey.length === 64) {
|
} else if (seckey.length === 64) {
|
||||||
// 纯 crypto
|
// 纯 crypto
|
||||||
let seckeyPEM = await new keyman.Key('oct', this.hex_to_buf(seckey), { namedCurve: 'P-256K' }).export('pem') // 私钥导出的der格式为144字节。
|
let seckeyPEM = await new keyman.Key('oct', this.hex_to_buf(seckey), { namedCurve: 'P-256K' }).export('pem') // 私钥导出的der格式为144字节。
|
||||||
let signer = crypto.createSign(my.HASHER_LIST.includes(hasher) ? hasher : my.HASHER) // 注意,不知为何,hasher必须含有'sha'才能完成签名,例如 sha1, sha256, sha512, sha3, RSA-SHA1, id-rsassa-pkcs1-v1_5-with-sha3-224, 其他都会报错。
|
let signer = crypto.createSign(my.HASHER_LIST.includes(hasher) ? hasher : my.HASHER) // 注意,不知为何,hasher必须含有'sha'才能完成签名,例如 sha1, sha256, sha512, sha3, RSA-SHA1, id-rsassa-pkcs1-v1_5-with-sha3-224, 其他都会报错。
|
||||||
signer.update(this.hash(data)).end()
|
signer.update(this.hash_easy(data)).end()
|
||||||
let signature = signer.sign(seckeyPEM, 'hex')
|
let signature = signer.sign(seckeyPEM, 'hex')
|
||||||
// since nodejs 12, 有了 crypto.sign 方法,但在浏览器中无效:
|
// since nodejs 12, 有了 crypto.sign 方法,但在浏览器中无效:
|
||||||
// let signature = crypto.sign(my.HASHER_LIST.includes(hasher) ? hasher : my.HASHER, Buffer.from(this.hash(data)), seckeyPEM).toString('hex')
|
// let signature = crypto.sign(my.HASHER_LIST.includes(hasher) ? hasher : my.HASHER, Buffer.from(this.hash_easy(data)), seckeyPEM).toString('hex')
|
||||||
return signature // 发现同样的输入,nodejs里每次调用会生成不同的 signature, 且长度不定(140,142,144 hex) 但都可以通过 verify。但在浏览器里调用,signature却是固定的。
|
return signature // 发现同样的输入,nodejs里每次调用会生成不同的 signature, 且长度不定(140,142,144 hex) 但都可以通过 verify。但在浏览器里调用,signature却是固定的。
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -338,7 +338,7 @@ class TicCrypto {
|
|||||||
// 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
|
||||||
if (this.is_hashable({ data }) && this.is_signature(signature) && this.is_pubkey({ pubkey })) {
|
if (this.is_hashable({ data }) && this.is_signature(signature) && this.is_pubkey({ pubkey })) {
|
||||||
if ('nacl' === tool && signature.length === 128) {
|
if ('nacl' === tool && signature.length === 128) {
|
||||||
let bufHash = this.hash(data, { output: 'buf' })
|
let bufHash = this.hash_easy(data, { output: 'buf' })
|
||||||
let bufSignature = Buffer.from(signature, 'hex')
|
let bufSignature = Buffer.from(signature, 'hex')
|
||||||
let bufPubkey = Buffer.from(pubkey, 'hex')
|
let bufPubkey = Buffer.from(pubkey, 'hex')
|
||||||
let verified = nacl.sign.detached.verify(bufHash, bufSignature, bufPubkey)
|
let verified = nacl.sign.detached.verify(bufHash, bufSignature, bufPubkey)
|
||||||
@ -346,7 +346,7 @@ class TicCrypto {
|
|||||||
} else if ('eccrypto' === tool && signature.length >= 140) {
|
} else if ('eccrypto' === tool && signature.length >= 140) {
|
||||||
// 默认使用 eccrypto // 发现大小写不影响 eccrypto 验签!都能通过
|
// 默认使用 eccrypto // 发现大小写不影响 eccrypto 验签!都能通过
|
||||||
try {
|
try {
|
||||||
let result = await eccrypto.verify(Buffer.from(pubkey, 'hex'), this.hash(data, { output: 'buf' }), Buffer.from(signature, 'hex')) // 如果给signature添加1位hex,eccrypto 的 verify结果也是true! 估计因为一位hex不被转成字节。
|
let result = await eccrypto.verify(Buffer.from(pubkey, 'hex'), this.hash_easy(data, { output: 'buf' }), Buffer.from(signature, 'hex')) // 如果给signature添加1位hex,eccrypto 的 verify结果也是true! 估计因为一位hex不被转成字节。
|
||||||
return true
|
return true
|
||||||
} catch (exception) {
|
} catch (exception) {
|
||||||
// 对能够验证的,eccrypto返回 null;对无法验证的,抛出异常
|
// 对能够验证的,eccrypto返回 null;对无法验证的,抛出异常
|
||||||
@ -356,10 +356,10 @@ class TicCrypto {
|
|||||||
// 纯 crypto // 发现大小写不影响 crypto 验签!都能通过
|
// 纯 crypto // 发现大小写不影响 crypto 验签!都能通过
|
||||||
let pubkeyPEM = await new keyman.Key('oct', this.hex_to_buf(pubkey), { namedCurve: 'P-256K' }).export('pem') // 公钥导出的der格式为88字节。经测试,同一对压缩和非压缩公钥得出的结果一模一样。
|
let pubkeyPEM = await new keyman.Key('oct', this.hex_to_buf(pubkey), { namedCurve: 'P-256K' }).export('pem') // 公钥导出的der格式为88字节。经测试,同一对压缩和非压缩公钥得出的结果一模一样。
|
||||||
let verifier = crypto.createVerify(my.HASHER_LIST.includes(hasher) ? hasher : my.HASHER)
|
let verifier = crypto.createVerify(my.HASHER_LIST.includes(hasher) ? hasher : my.HASHER)
|
||||||
verifier.update(this.hash(data)).end() // end() 在 nodejs 12 里返回verifier自身,但在浏览器里返回 undefined,因此不能串联运行。
|
verifier.update(this.hash_easy(data)).end() // end() 在 nodejs 12 里返回verifier自身,但在浏览器里返回 undefined,因此不能串联运行。
|
||||||
let verified = verifier.verify(pubkeyPEM, signature, 'hex') // 如果给signature添加1位hex,crypto 的 verify结果也是true! 估计因为一位hex不被转成字节。但减少1位会导致false
|
let verified = verifier.verify(pubkeyPEM, signature, 'hex') // 如果给signature添加1位hex,crypto 的 verify结果也是true! 估计因为一位hex不被转成字节。但减少1位会导致false
|
||||||
// since nodejs 12, 有了 crypto.verify 方法,但在浏览器中无效:
|
// since nodejs 12, 有了 crypto.verify 方法,但在浏览器中无效:
|
||||||
// let verified = crypto.verify(my.HASHER_LIST.includes(hasher) ? hasher : my.HASHER, Buffer.from(this.hash(data)), pubkeyPEM, Buffer.from(signature, 'hex'))
|
// let verified = crypto.verify(my.HASHER_LIST.includes(hasher) ? hasher : my.HASHER, Buffer.from(this.hash_easy(data)), pubkeyPEM, Buffer.from(signature, 'hex'))
|
||||||
return verified
|
return verified
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -501,7 +501,7 @@ class TicCrypto {
|
|||||||
// 后面还可继续延伸 /xxx/xxx/xxx/......
|
// 后面还可继续延伸 /xxx/xxx/xxx/......
|
||||||
let path
|
let path
|
||||||
if (pathRoot) {
|
if (pathRoot) {
|
||||||
let pathHash = this.hash(pathRoot, { hasher: 'md5' })
|
let pathHash = this.hash_easy(pathRoot, { hasher: 'md5' })
|
||||||
let part0 = parseInt(pathHash.slice(0, 6), 16)
|
let part0 = parseInt(pathHash.slice(0, 6), 16)
|
||||||
let part1 = parseInt(pathHash.slice(6, 12), 16)
|
let part1 = parseInt(pathHash.slice(6, 12), 16)
|
||||||
let part2 = parseInt(pathHash.slice(12, 18), 16)
|
let part2 = parseInt(pathHash.slice(12, 18), 16)
|
||||||
@ -758,7 +758,7 @@ class TicCrypto {
|
|||||||
default:
|
default:
|
||||||
prefix = '4c'
|
prefix = '4c'
|
||||||
}
|
}
|
||||||
let checksum = this.hash(this.hash(prefix + position)).slice(0, 6) // 添加 checksum 使得能够检测大小写错误。[todo] 校验码里要不要包含 prefix?
|
let checksum = this.hash_easy(this.hash_easy(prefix + position)).slice(0, 6) // 添加 checksum 使得能够检测大小写错误。[todo] 校验码里要不要包含 prefix?
|
||||||
// address = this.hex_to_eip55(prefix + position + checksum) // 前缀1节,位置20节,校验3节,共24节=48字符(能够完全转化为8个色彩),再转eip55。
|
// address = this.hex_to_eip55(prefix + position + checksum) // 前缀1节,位置20节,校验3节,共24节=48字符(能够完全转化为8个色彩),再转eip55。
|
||||||
address = this.hex_to_b64t(prefix + position + checksum) // 实际采用 b64t, 共 32字符。
|
address = this.hex_to_b64t(prefix + position + checksum) // 实际采用 b64t, 共 32字符。
|
||||||
return address
|
return address
|
||||||
@ -790,7 +790,7 @@ class TicCrypto {
|
|||||||
// 格式合法
|
// 格式合法
|
||||||
let hex = this.b64t_to_hex(address)
|
let hex = this.b64t_to_hex(address)
|
||||||
let [all, prefix, position, checksum] = hex.match(/^([\da-fA-F]{2})([\da-fA-F]{40})([\da-fA-F]{6})$/)
|
let [all, prefix, position, checksum] = hex.match(/^([\da-fA-F]{2})([\da-fA-F]{40})([\da-fA-F]{6})$/)
|
||||||
if (this.hash(this.hash(position)).slice(0, 6) === checksum) {
|
if (this.hash_easy(this.hash_easy(position)).slice(0, 6) === checksum) {
|
||||||
return position
|
return position
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -818,7 +818,7 @@ class TicCrypto {
|
|||||||
// 格式合法
|
// 格式合法
|
||||||
let hex = Buffer.from(this.b64t_to_b64(address), 'base64').toString('hex')
|
let hex = Buffer.from(this.b64t_to_b64(address), 'base64').toString('hex')
|
||||||
let [all, prefix, position, checksum] = hex.match(/^([\da-fA-F]{2})([\da-fA-F]{40})([\da-fA-F]{6})$/) // 内容合法
|
let [all, prefix, position, checksum] = hex.match(/^([\da-fA-F]{2})([\da-fA-F]{40})([\da-fA-F]{6})$/) // 内容合法
|
||||||
if (this.hash(this.hash(prefix + position)).slice(0, 6) === checksum)
|
if (this.hash_easy(this.hash_easy(prefix + position)).slice(0, 6) === checksum)
|
||||||
// [todo] 校验码里要不要包含 prefix?
|
// [todo] 校验码里要不要包含 prefix?
|
||||||
return 'TIC'
|
return 'TIC'
|
||||||
}
|
}
|
||||||
@ -1046,13 +1046,13 @@ class TicCrypto {
|
|||||||
hashList = [...hashList]
|
hashList = [...hashList]
|
||||||
if (!Array.isArray(hashList)) return null
|
if (!Array.isArray(hashList)) return null
|
||||||
var border = hashList.length
|
var border = hashList.length
|
||||||
if (border == 0) return this.hash('')
|
if (border == 0) return this.hash_easy('')
|
||||||
if (border == 1) return this.hash(hashList[0])
|
if (border == 1) return this.hash_easy(hashList[0])
|
||||||
while (1) {
|
while (1) {
|
||||||
let i = 1,
|
let i = 1,
|
||||||
j = 0
|
j = 0
|
||||||
for (; i < border; i = i + 2) {
|
for (; i < border; i = i + 2) {
|
||||||
hashList[j] = this.hash(hashList[i - 1] + hashList[i])
|
hashList[j] = this.hash_easy(hashList[i - 1] + hashList[i])
|
||||||
if (border == 2) {
|
if (border == 2) {
|
||||||
return hashList[0]
|
return hashList[0]
|
||||||
}
|
}
|
||||||
@ -1060,7 +1060,7 @@ class TicCrypto {
|
|||||||
j = j + 1
|
j = j + 1
|
||||||
if (i + 2 == border) {
|
if (i + 2 == border) {
|
||||||
i = i + 1
|
i = i + 1
|
||||||
hashList[j] = this.hash(hashList[i])
|
hashList[j] = this.hash_easy(hashList[i])
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1081,7 +1081,7 @@ class TicCrypto {
|
|||||||
static hash_to_sig_distance ({ hash, sig } = {}) {
|
static hash_to_sig_distance ({ hash, sig } = {}) {
|
||||||
// hash为64hex字符,sig为128hex字符。返回用hex表达的距离。
|
// hash为64hex字符,sig为128hex字符。返回用hex表达的距离。
|
||||||
if (this.is_signature(sig) && this.is_hash({ hash })) {
|
if (this.is_signature(sig) && this.is_hash({ hash })) {
|
||||||
var hashSig = this.hash(sig) // 把签名也转成32字节的哈希,同样长度方便比较
|
var hashSig = this.hash_easy(sig) // 把签名也转成32字节的哈希,同样长度方便比较
|
||||||
return new BigInt(hash, 16)
|
return new BigInt(hash, 16)
|
||||||
.subtract(new BigInt(hashSig, 16))
|
.subtract(new BigInt(hashSig, 16))
|
||||||
.abs()
|
.abs()
|
||||||
@ -1508,7 +1508,7 @@ class TicCrypto {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static string_to_raw_cid (str) {
|
static string_to_raw_cid (str) {
|
||||||
return this.cosh_to_cid({ cosh: this.hash(str), cidVersion: 1, cidCodec: 'raw' })
|
return this.cosh_to_cid({ cosh: this.hash_easy(str), cidVersion: 1, cidCodec: 'raw' })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user