Skip to content

Commit

Permalink
Separate function for unslabbedHash
Browse files Browse the repository at this point in the history
  • Loading branch information
HDegroote committed Sep 18, 2024
1 parent f4f5430 commit 86900b7
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
4 changes: 2 additions & 2 deletions lib/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const SecurePayload = require('./secure-payload')
const Holepuncher = require('./holepuncher')
const Sleeper = require('./sleeper')
const { FIREWALL, ERROR } = require('./constants')
const { hash } = require('./crypto')
const { unslabbedHash } = require('./crypto')
const {
CANNOT_HOLEPUNCH,
HANDSHAKE_INVALID,
Expand Down Expand Up @@ -63,7 +63,7 @@ module.exports = function connect (dht, publicKey, opts = {}) {
relayAddresses: opts.relayAddresses || [],
pool,
round: 0,
target: hash(publicKey, b4a.allocUnsafeSlow),
target: unslabbedHash(publicKey),
remotePublicKey: publicKey,
reusableSocket: !!opts.reusableSocket,
handshake: (opts.createHandshake || defaultCreateHandshake)(keyPair, publicKey),
Expand Down
11 changes: 9 additions & 2 deletions lib/crypto.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
const sodium = require('sodium-universal')
const b4a = require('b4a')

function hash (data, alloc = b4a.allocUnsafe) {
const out = alloc(32)
function hash (data) {
const out = b4a.allocUnsafe(32)
sodium.crypto_generichash(out, data)
return out
}

function unslabbedHash (data) {
const out = b4a.allocUnsafeSlow(32)
sodium.crypto_generichash(out, data)
return out
}
Expand All @@ -17,5 +23,6 @@ function createKeyPair (seed) {

module.exports = {
hash,
unslabbedHash,
createKeyPair
}
4 changes: 2 additions & 2 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const relay = require('blind-relay')
const NoiseWrap = require('./noise-wrap')
const Announcer = require('./announcer')
const { FIREWALL, ERROR } = require('./constants')
const { hash } = require('./crypto')
const { unslabbedHash } = require('./crypto')
const SecurePayload = require('./secure-payload')
const Holepuncher = require('./holepuncher')
const DebuggingStream = require('debugging-stream')
Expand Down Expand Up @@ -164,7 +164,7 @@ module.exports = class Server extends EventEmitter {
await this.dht.bind()
if (this._closing) return

this.target = hash(keyPair.publicKey, b4a.allocUnsafeSlow)
this.target = unslabbedHash(keyPair.publicKey)

this._keyPair = keyPair
this._announcer = new Announcer(this.dht, keyPair, this.target, opts)
Expand Down

0 comments on commit 86900b7

Please sign in to comment.