Skip to content

Instantly share code, notes, and snippets.

@emilbayes
Created March 20, 2020 12:01
Show Gist options
  • Save emilbayes/2be519c110054034501de44682bd7533 to your computer and use it in GitHub Desktop.
Save emilbayes/2be519c110054034501de44682bd7533 to your computer and use it in GitHub Desktop.
DHT with sodium-native
const sodium = require('sodium-native')
const DHT = require('bittorrent-dht')
const dht = new DHT({
verify: sodium.crypto_sign_verify_detached
})
const keys = keygen()
dht.put({
v: Buffer.from('Hello world'),
k: keys.pk,
seq: 0,
sign: (msg) => {
const sig = Buffer.alloc(sodium.crypto_sign_BYTES)
sodium.crypto_sign_detached(sig, msg, keys.sk)
return sig
}
}, function (err, prev) {
// ...
})
// Optionally pass an exisiting secret key and rederive the public key
function keygen (sk) {
const pk = Buffer.alloc(sodium.crypto_sign_PUBLICKEYBYTES)
if (sk == null) {
sk = sodium.sodium_malloc(sodium.crypto_sign_SECRETKEYBYTES)
sodium.crypto_sign_keypair(pk, sk)
} else {
sodium.crypto_sign_ed25519_sk_to_pk(pk, sk)
}
return { pk, sk }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment