hypercore-crypto
Advanced tools
Comparing version 2.3.1 to 2.3.2
65
index.js
const sodium = require('sodium-universal') | ||
const uint64be = require('uint64be') | ||
const b4a = require('b4a') | ||
// https://en.wikipedia.org/wiki/Merkle_tree#Second_preimage_attack | ||
const LEAF_TYPE = b4a.from([0]) | ||
const PARENT_TYPE = b4a.from([1]) | ||
const ROOT_TYPE = b4a.from([2]) | ||
const CAP_TYPE = b4a.from([3]) | ||
const LEAF_TYPE = Buffer.from([0]) | ||
const PARENT_TYPE = Buffer.from([1]) | ||
const ROOT_TYPE = Buffer.from([2]) | ||
const CAP_TYPE = Buffer.from([3]) | ||
const HYPERCORE = b4a.from('hypercore') | ||
const HYPERCORE_CAP = b4a.from('hypercore capability') | ||
const HYPERCORE = Buffer.from('hypercore') | ||
const HYPERCORE_CAP = Buffer.from('hypercore capability') | ||
@@ -17,9 +16,9 @@ exports.writerCapability = function (key, secretKey, split) { | ||
const out = b4a.allocUnsafe(32) | ||
const out = Buffer.allocUnsafe(32) | ||
sodium.crypto_generichash_batch(out, [ | ||
CAP_TYPE, | ||
HYPERCORE_CAP, | ||
split.tx.subarray(0, 32), | ||
split.tx.slice(0, 32), | ||
key | ||
], split.rx.subarray(0, 32)) | ||
], split.rx.slice(0, 32)) | ||
@@ -32,9 +31,9 @@ return exports.sign(out, secretKey) | ||
const out = b4a.allocUnsafe(32) | ||
const out = Buffer.allocUnsafe(32) | ||
sodium.crypto_generichash_batch(out, [ | ||
CAP_TYPE, | ||
HYPERCORE_CAP, | ||
split.rx.subarray(0, 32), | ||
split.rx.slice(0, 32), | ||
key | ||
], split.tx.subarray(0, 32)) | ||
], split.tx.slice(0, 32)) | ||
@@ -48,8 +47,8 @@ return exports.verify(out, cap, key) | ||
const out = b4a.allocUnsafe(32) | ||
const out = Buffer.allocUnsafe(32) | ||
sodium.crypto_generichash_batch(out, [ | ||
HYPERCORE_CAP, | ||
split.tx.subarray(0, 32), | ||
split.tx.slice(0, 32), | ||
key | ||
], split.rx.subarray(0, 32)) | ||
], split.rx.slice(0, 32)) | ||
@@ -63,8 +62,8 @@ return out | ||
const out = b4a.allocUnsafe(32) | ||
const out = Buffer.allocUnsafe(32) | ||
sodium.crypto_generichash_batch(out, [ | ||
HYPERCORE_CAP, | ||
split.rx.subarray(0, 32), | ||
split.rx.slice(0, 32), | ||
key | ||
], split.tx.subarray(0, 32)) | ||
], split.tx.slice(0, 32)) | ||
@@ -75,4 +74,4 @@ return out | ||
exports.keyPair = function (seed) { | ||
const publicKey = b4a.allocUnsafe(sodium.crypto_sign_PUBLICKEYBYTES) | ||
const secretKey = b4a.allocUnsafe(sodium.crypto_sign_SECRETKEYBYTES) | ||
const publicKey = Buffer.allocUnsafe(sodium.crypto_sign_PUBLICKEYBYTES) | ||
const secretKey = Buffer.allocUnsafe(sodium.crypto_sign_SECRETKEYBYTES) | ||
@@ -89,3 +88,3 @@ if (seed) sodium.crypto_sign_seed_keypair(publicKey, secretKey, seed) | ||
exports.validateKeyPair = function (keyPair) { | ||
const pk = b4a.allocUnsafe(sodium.crypto_sign_PUBLICKEYBYTES) | ||
const pk = Buffer.allocUnsafe(sodium.crypto_sign_PUBLICKEYBYTES) | ||
sodium.crypto_sign_ed25519_sk_to_pk(pk, keyPair.secretKey) | ||
@@ -96,3 +95,3 @@ return pk.equals(keyPair.publicKey) | ||
exports.sign = function (message, secretKey) { | ||
const signature = b4a.allocUnsafe(sodium.crypto_sign_BYTES) | ||
const signature = Buffer.allocUnsafe(sodium.crypto_sign_BYTES) | ||
sodium.crypto_sign_detached(signature, message, secretKey) | ||
@@ -107,3 +106,3 @@ return signature | ||
exports.data = function (data) { | ||
const out = b4a.allocUnsafe(32) | ||
const out = Buffer.allocUnsafe(32) | ||
@@ -130,3 +129,3 @@ sodium.crypto_generichash_batch(out, [ | ||
const out = b4a.allocUnsafe(32) | ||
const out = Buffer.allocUnsafe(32) | ||
@@ -156,3 +155,3 @@ sodium.crypto_generichash_batch(out, [ | ||
if (!out) out = b4a.allocUnsafe(32) | ||
if (!out) out = Buffer.allocUnsafe(32) | ||
sodium.crypto_generichash_batch(out, buffers) | ||
@@ -163,8 +162,8 @@ return out | ||
exports.signable = function (roots, length) { | ||
const out = b4a.allocUnsafe(40) | ||
const out = Buffer.allocUnsafe(40) | ||
if (b4a.isBuffer(roots)) b4a.copy(roots, out) | ||
else exports.tree(roots, out.subarray(0, 32)) | ||
if (Buffer.isBuffer(roots)) roots.copy(out) | ||
else exports.tree(roots, out.slice(0, 32)) | ||
uint64be.encode(length, out.subarray(32)) | ||
uint64be.encode(length, out.slice(32)) | ||
@@ -175,3 +174,3 @@ return out | ||
exports.randomBytes = function (n) { | ||
const buf = b4a.allocUnsafe(n) | ||
const buf = Buffer.allocUnsafe(n) | ||
sodium.randombytes_buf(buf) | ||
@@ -182,3 +181,3 @@ return buf | ||
exports.discoveryKey = function (publicKey) { | ||
const digest = b4a.allocUnsafe(32) | ||
const digest = Buffer.allocUnsafe(32) | ||
sodium.crypto_generichash(digest, HYPERCORE, publicKey) | ||
@@ -197,3 +196,3 @@ return digest | ||
function encodeUInt64 (n) { | ||
return uint64be.encode(n, b4a.allocUnsafe(8)) | ||
return uint64be.encode(n, Buffer.allocUnsafe(8)) | ||
} |
{ | ||
"name": "hypercore-crypto", | ||
"version": "2.3.1", | ||
"version": "2.3.2", | ||
"description": "The crypto primitives used in hypercore, extracted into a separate module", | ||
"main": "index.js", | ||
"dependencies": { | ||
"b4a": "^1.1.0", | ||
"sodium-universal": "^3.0.0", | ||
@@ -9,0 +8,0 @@ "uint64be": "^3.0.0" |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
2
10532
212
- Removedb4a@^1.1.0