Socket
Socket
Sign inDemoInstall

hypercore-crypto

Package Overview
Dependencies
Maintainers
2
Versions
21
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

hypercore-crypto - npm Package Compare versions

Comparing version 3.0.0 to 3.1.0

91

index.js
const sodium = require('sodium-universal')
const uint64be = require('uint64be')
const c = require('compact-encoding')
const b4a = require('b4a')

@@ -9,63 +9,5 @@

const ROOT_TYPE = b4a.from([2])
const CAP_TYPE = b4a.from([3])
const HYPERCORE = b4a.from('hypercore')
const HYPERCORE_CAP = b4a.from('hypercore capability')
exports.writerCapability = function (key, secretKey, split) {
if (!split) return null
const out = b4a.allocUnsafe(32)
sodium.crypto_generichash_batch(out, [
CAP_TYPE,
HYPERCORE_CAP,
split.tx.subarray(0, 32),
key
], split.rx.subarray(0, 32))
return exports.sign(out, secretKey)
}
exports.verifyRemoteWriterCapability = function (key, cap, split) {
if (!split) return null
const out = b4a.allocUnsafe(32)
sodium.crypto_generichash_batch(out, [
CAP_TYPE,
HYPERCORE_CAP,
split.rx.subarray(0, 32),
key
], split.tx.subarray(0, 32))
return exports.verify(out, cap, key)
}
// TODO: add in the CAP_TYPE in a future version
exports.capability = function (key, split) {
if (!split) return null
const out = b4a.allocUnsafe(32)
sodium.crypto_generichash_batch(out, [
HYPERCORE_CAP,
split.tx.subarray(0, 32),
key
], split.rx.subarray(0, 32))
return out
}
// TODO: add in the CAP_TYPE in a future version
exports.remoteCapability = function (key, split) {
if (!split) return null
const out = b4a.allocUnsafe(32)
sodium.crypto_generichash_batch(out, [
HYPERCORE_CAP,
split.rx.subarray(0, 32),
key
], split.tx.subarray(0, 32))
return out
}
exports.keyPair = function (seed) {

@@ -105,3 +47,3 @@ const publicKey = b4a.allocUnsafe(sodium.crypto_sign_PUBLICKEYBYTES)

LEAF_TYPE,
encodeUInt64(data.length),
c.encode(c.uint64, data.byteLength),
data

@@ -113,6 +55,2 @@ ])

exports.leaf = function (leaf) {
return exports.data(leaf.data)
}
exports.parent = function (a, b) {

@@ -129,3 +67,3 @@ if (a.index > b.index) {

PARENT_TYPE,
encodeUInt64(a.size + b.size),
c.encode(c.uint64, a.size + b.size),
a.hash,

@@ -140,11 +78,11 @@ b.hash

const buffers = new Array(3 * roots.length + 1)
var j = 0
let j = 0
buffers[j++] = ROOT_TYPE
for (var i = 0; i < roots.length; i++) {
for (let i = 0; i < roots.length; i++) {
const r = roots[i]
buffers[j++] = r.hash
buffers[j++] = encodeUInt64(r.index)
buffers[j++] = encodeUInt64(r.size)
buffers[j++] = c.encode(c.uint64, r.index)
buffers[j++] = c.encode(c.uint64, r.size)
}

@@ -157,13 +95,2 @@

exports.signable = function (roots, length) {
const out = b4a.allocUnsafe(40)
if (b4a.isBuffer(roots)) b4a.copy(roots, out)
else exports.tree(roots, out.subarray(0, 32))
uint64be.encode(length, out.subarray(32))
return out
}
exports.randomBytes = function (n) {

@@ -188,5 +115,1 @@ const buf = b4a.allocUnsafe(n)

}
function encodeUInt64 (n) {
return uint64be.encode(n, b4a.allocUnsafe(8))
}

6

package.json
{
"name": "hypercore-crypto",
"version": "3.0.0",
"version": "3.1.0",
"description": "The crypto primitives used in hypercore, extracted into a separate module",

@@ -8,4 +8,4 @@ "main": "index.js",

"b4a": "^1.1.0",
"sodium-universal": "^3.0.0",
"uint64be": "^3.0.0"
"compact-encoding": "^2.5.1",
"sodium-universal": "^3.0.0"
},

@@ -12,0 +12,0 @@ "devDependencies": {

# hypercore-crypto
> :warning: Hypercore 9 uses v2 of this module while Hypercore 10 uses v3.
The crypto primitives used in hypercore, extracted into a separate module

@@ -52,6 +54,2 @@

#### `dataToSign = crypto.signable(peaksOrHash, length)`
Encodes a buffer to sign. `length` should be how many leaf nodes exist in the tree.
#### `buffer = crypto.randomBytes(size)`

@@ -58,0 +56,0 @@

@@ -43,3 +43,3 @@ const tape = require('tape')

t.same(crypto.data(data), Buffer.from('ccfa4259ee7c41e411e5770973a49c5ceffb5272d6a37f2c6f2dac2190f7e2b7', 'hex'))
t.same(crypto.data(data), Buffer.from('9f1b578fd57a4df015493d2886aec9600eef913c3bb009768c7f0fb875996308', 'hex'))
t.end()

@@ -61,18 +61,6 @@ })

t.same(parent, Buffer.from('43563406adba8b34b133fdca32d0a458c5be769615e01df30e6535ccd3c075f0', 'hex'))
t.same(parent, Buffer.from('3ad0c9b58b771d1b7707e1430f37c23a23dd46e0c7c3ab9c16f79d25f7c36804', 'hex'))
t.end()
})
tape('capabilities', function (t) {
const key = Buffer.alloc(32).fill('secret')
const local = { rx: Buffer.alloc(32).fill('rx'), tx: Buffer.alloc(32).fill('tx') }
const remote = { rx: local.tx, tx: local.rx }
const cap = crypto.capability(key, local)
const remoteCap = crypto.remoteCapability(key, remote)
t.same(cap, remoteCap)
t.end()
})
tape('tree', function (t) {

@@ -84,6 +72,4 @@ const roots = [

t.same(crypto.tree(roots), Buffer.from('334dd9d8f9a48c7b7e60affa8704a3597f87fe645fe83f1aada3a1216ea91e65', 'hex'))
t.same(crypto.signable(Buffer.from('334dd9d8f9a48c7b7e60affa8704a3597f87fe645fe83f1aada3a1216ea91e65', 'hex'), 6), Buffer.from('334dd9d8f9a48c7b7e60affa8704a3597f87fe645fe83f1aada3a1216ea91e650000000000000006', 'hex'))
t.same(crypto.signable(roots, 6), Buffer.from('334dd9d8f9a48c7b7e60affa8704a3597f87fe645fe83f1aada3a1216ea91e650000000000000006', 'hex'))
t.same(crypto.tree(roots), Buffer.from('0e576a56b478cddb6ffebab8c494532b6de009466b2e9f7af9143fc54b9eaa36', 'hex'))
t.end()
})
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc