Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
fs.readFile('./test/fixtures/example.txt.minisig', (err, signature) => {
t.error(err)
var sigInfo = minisign.parseSignature(signature)
t.equal(sigInfo.signature.length, sodium.crypto_sign_BYTES)
t.deepEqual(sigInfo.signatureAlgorithm, Buffer.from('Ed'))
t.equal(sigInfo.keyID.byteLength, 8)
t.equal(sigInfo.globalSignature.length, sodium.crypto_sign_BYTES)
t.end()
})
})
fs.readFile('./test/fixtures/pre-hashed.txt.minisig', (err, signature) => {
t.error(err)
var sigInfo = minisign.parseSignature(signature)
t.equal(sigInfo.signature.length, sodium.crypto_sign_BYTES)
t.deepEqual(sigInfo.signatureAlgorithm, Buffer.from('ED'))
t.equal(sigInfo.keyID.byteLength, 8)
t.equal(sigInfo.globalSignature.length, sodium.crypto_sign_BYTES)
t.end()
})
})
export const signDetached: NaclInterface['signDetached'] = (
messageBytes,
privateKeyBytes,
) => {
const signatureBytes = Buffer.alloc(sodium.crypto_sign_BYTES);
sodium.crypto_sign_detached(signatureBytes, messageBytes, privateKeyBytes);
return signatureBytes;
};
public sign(hash: Buffer, keyPair: IKeyPair): Buffer {
const sig: Buffer = Buffer.alloc(sodium.crypto_sign_BYTES);
sodium.crypto_sign_detached(sig, hash, keyPair.privateKey);
return sig;
}
function signContent (content, comment, SKdetails, trustComment, sigAlgorithm = 'Ed') {
var contentToSign
var signatureAlgorithm
if (sigAlgorithm === 'ED') {
var hashedContent = Buffer.alloc(sodium.crypto_generichash_BYTES_MAX)
sodium.crypto_generichash(hashedContent, content)
contentToSign = hashedContent
signatureAlgorithm = Buffer.from(sigAlgorithm)
} else {
contentToSign = content
signatureAlgorithm = Buffer.from(SKdetails.signatureAlgorithm)
}
var signature = Buffer.alloc(sodium.crypto_sign_BYTES)
var globalSignature = Buffer.alloc(sodium.crypto_sign_BYTES)
sodium.crypto_sign_detached(signature, contentToSign, SKdetails.secretKey)
var signatureInfo = Buffer.concat([signatureAlgorithm, SKdetails.keyID, signature])
var untrustedComment = ('untrusted comment: ' + comment + '\n')
var trustedComment = ('trusted comment: ' + trustComment.toString('ascii') + '\n')
var forGlobalSig = Buffer.concat([signature, Buffer.from(trustComment)])
sodium.crypto_sign_detached(globalSignature, forGlobalSig, SKdetails.secretKey)
var minisignStr = (untrustedComment + signatureInfo.toString('base64') + '\n' + trustedComment + globalSignature.toString('base64'))
return Buffer.from(minisignStr)
}
var contentToSign
var signatureAlgorithm
var trustComment
if (sigAlgorithm.equals(Buffer.from('ED'))) {
var hashedContent = Buffer.alloc(sodium.crypto_generichash_BYTES_MAX)
sodium.crypto_generichash(hashedContent, content)
contentToSign = hashedContent
signatureAlgorithm = Buffer.from(sigAlgorithm)
} else {
assert(sigAlgorithm.equals(Buffer.from('Ed')), 'algorithm not recognised')
contentToSign = content
signatureAlgorithm = Buffer.from(SKdetails.signatureAlgorithm)
}
var signature = Buffer.alloc(sodium.crypto_sign_BYTES)
var globalSignature = Buffer.alloc(sodium.crypto_sign_BYTES)
sodium.sodium_mprotect_readwrite(SKdetails.secretKey)
sodium.crypto_sign_detached(signature, contentToSign, SKdetails.secretKey)
var signatureInfo = Buffer.concat([signatureAlgorithm, SKdetails.keyID, signature])
var untrustedComment = Buffer.from('untrusted comment: ' + comment + '\n')
var trustedComment = Buffer.from('\ntrusted comment: ' + tComment + '\n')
var sigInfoBase64 = Buffer.from(signatureInfo.toString('base64'))
var forGlobalSig = Buffer.concat([signature, Buffer.from(tComment)])
sodium.crypto_sign_detached(globalSignature, forGlobalSig, SKdetails.secretKey)
sodium.sodium_memzero(SKdetails.secretKey)
sodium.sodium_mprotect_noaccess(SKdetails.secretKey)
var globalSigBase64 = Buffer.from(globalSignature.toString('base64') + '\n')
public sign(hash: Buffer, keypair: IKeypair): Buffer {
const signature = Buffer.alloc(sodium.crypto_sign_BYTES);
sodium.crypto_sign_detached(signature, hash, keypair.privateKey);
return signature;
}
export function sign(secretKey: EncodedSecretSigningKey, message: Buffer): EncodedSignature {
const secretKeyBuffer = decode(secretKey)
const signatureBuffer = Buffer.alloc(sodium.crypto_sign_BYTES) as sodium.Signature
sodium.crypto_sign_detached(signatureBuffer, message, secretKeyBuffer)
return encode(signatureBuffer)
}
sign: function (buf) {
var sig = Buffer.alloc(sodium.crypto_sign_BYTES)
sodium.crypto_sign_detached(sig, buf, discoverySk)
return sig
}
}, cb)