Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
test('minisign.js generated key', (t) => {
var emptyBuf = Buffer.from('')
var pwd = sodium.sodium_malloc(emptyBuf.byteLength)
pwd.fill(emptyBuf)
var keyGen = minisign.keypairGen(pwd)
var key = minisign.formatKeys(keyGen)
var PKinfo = minisign.parsePubKey(key.PK)
var formatKeyID = PKinfo.keyID.toString('hex').toUpperCase()
t.equals(PKinfo.untrustedComment.toString(), keyGen.PKcomment)
t.equals(formatKeyID, keyGen.PKcomment.slice(20, 36))
t.equals(PKinfo.publicKey.byteLength, sodium.crypto_sign_PUBLICKEYBYTES)
t.end()
})
fs.readFile('./test/fixtures/minisign.pub', (err, pubkey) => {
var PKinfo = minisign.parsePubKey(pubkey)
var formatKeyID = reverse(PKinfo.keyID).toString('hex').toUpperCase()
t.error(err)
t.equals(PKinfo.untrustedComment.toString(), comment)
t.equals(formatKeyID, comment.slice(20, 36))
t.equals(PKinfo.publicKey.byteLength, sodium.crypto_sign_PUBLICKEYBYTES)
t.end()
})
})
Rendezvous.prototype.write = function (remoteKey, message, cb) {
assert(remoteKey.byteLength === sodium.crypto_kx_PUBLICKEYBYTES)
assert(message.byteLength <= 1000)
var self = this
var rendevousPoint = Buffer.allocUnsafe(sodium.crypto_kx_SESSIONKEYBYTES)
sodium.crypto_kx_server_session_keys(
rendevousPoint,
null,
self.keypair.publicKey,
self.keypair.secretKey,
remoteKey
)
var discoveryPk = Buffer.alloc(sodium.crypto_sign_PUBLICKEYBYTES)
var discoverySk = sodium.sodium_malloc(sodium.crypto_sign_SECRETKEYBYTES)
sodium.crypto_sign_seed_keypair(discoveryPk, discoverySk, rendevousPoint)
self.dht.put({
v: message,
k: discoveryPk,
seq: 0,
sign: function (buf) {
var sig = Buffer.alloc(sodium.crypto_sign_BYTES)
sodium.crypto_sign_detached(sig, buf, discoverySk)
return sig
}
}, cb)
}
function keypairGen (comment, pwd, sigAlgorithm = 'Ed', kdfAlgorithm = 'Sc', cksumAlgorithm = 'B2') {
var keyID = Buffer.alloc(8)
var kdfSalt = Buffer.alloc(32)
var kdfOutput = Buffer.alloc(104)
var publicKey = Buffer.alloc(sodium.crypto_sign_PUBLICKEYBYTES)
var secretKey = Buffer.alloc(sodium.crypto_sign_SECRETKEYBYTES)
var checkSum = Buffer.alloc(sodium.crypto_generichash_BYTES)
var fullComment = Buffer.from('untrusted comment: ' + comment + '\n')
const kdfOpsLimit = sodium.crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_SENSITIVE
const kdfMemLimit = sodium.crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_SENSITIVE
sodium.randombytes_buf(keyID)
sodium.randombytes_buf(kdfSalt)
sodium.crypto_sign_keypair(publicKey, secretKey)
var checksumData = Buffer.concat([Buffer.from(sigAlgorithm), keyID, secretKey])
sodium.crypto_generichash(checkSum, checksumData)
var keynumData = Buffer.concat([keyID, secretKey, checkSum])
exports.keyPair = function (seed) {
var publicKey = new Buffer(sodium.crypto_sign_PUBLICKEYBYTES)
var secretKey = new Buffer(sodium.crypto_sign_SECRETKEYBYTES)
if (seed) sodium.crypto_sign_seed_keypair(publicKey, secretKey, seed)
else sodium.crypto_sign_keypair(publicKey, secretKey)
return {
publicKey: publicKey,
secretKey: secretKey
}
}
sodium.randombytes_buf(keyID)
var PKdComment = 'minisign public key ' + keyID.toString('hex').toUpperCase()
var SKdComment = 'minisign encrypted secret key'
if (opts == null) opts = {}
var PKcomment = opts.PKcomment || PKdComment
var SKcomment = opts.SKcomment || SKdComment
var sigAlgorithm = Buffer.from(opts.sigAlgorithm || 'Ed')
var kdfAlgorithm = Buffer.from(opts.kdfAlgorithm || 'Sc')
var cksumAlgorithm = Buffer.from(opts.cksumAlgorithm || 'B2')
var kdfSalt = Buffer.alloc(32)
var kdfOutput = Buffer.alloc(104)
var publicKey = Buffer.alloc(sodium.crypto_sign_PUBLICKEYBYTES)
var secretKey = Buffer.alloc(sodium.crypto_sign_SECRETKEYBYTES)
var checkSum = Buffer.alloc(sodium.crypto_generichash_BYTES)
sodium.randombytes_buf(kdfSalt)
sodium.crypto_sign_keypair(publicKey, secretKey)
const kdfOpsLimit = sodium.crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_SENSITIVE
const kdfMemLimit = sodium.crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_SENSITIVE
var kdfLimits = Buffer.alloc(16)
kdfLimits.writeUInt32LE(kdfOpsLimit, 0)
kdfLimits.writeUInt32LE(kdfMemLimit, 8)
var checkSumData = Buffer.concat([sigAlgorithm, keyID, secretKey])
sodium.crypto_generichash(checkSum, checkSumData)
var keynumData = Buffer.concat([keyID, secretKey, checkSum])
export function signingKeyPair(): sodium.SigningKeyPair {
const publicKey = Buffer.alloc(sodium.crypto_sign_PUBLICKEYBYTES) as sodium.PublicSigningKey
const secretKey = Buffer.alloc(sodium.crypto_sign_SECRETKEYBYTES) as sodium.SecretSigningKey
sodium.crypto_sign_keypair(publicKey, secretKey)
return { publicKey, secretKey }
}
public makeKeyPair(hash: Buffer): IKeyPair {
const keyPair: IKeyPair = {
publicKey: Buffer.alloc(sodium.crypto_sign_PUBLICKEYBYTES),
privateKey: Buffer.alloc(sodium.crypto_sign_SECRETKEYBYTES)
};
sodium.crypto_sign_seed_keypair(keyPair.publicKey, keyPair.privateKey, hash);
return keyPair;
}
public makeKeyPair(hash: Buffer): IKeypair {
const publicKey = Buffer.alloc(
sodium.crypto_sign_PUBLICKEYBYTES
) as Buffer & As<'publicKey'>;
const privateKey = Buffer.alloc(
sodium.crypto_sign_SECRETKEYBYTES
) as Buffer & As<'privateKey'>;
sodium.crypto_sign_seed_keypair(publicKey, privateKey, hash);
return { privateKey, publicKey };
}