Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
secretKey.readable(_secretKey => {
sodium.crypto_sign_detached(out, message, _secretKey)
})
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)
}
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)
}
export const signDetached: NaclInterface['signDetached'] = (
messageBytes,
privateKeyBytes,
) => {
const signatureBytes = Buffer.alloc(sodium.crypto_sign_BYTES);
sodium.crypto_sign_detached(signatureBytes, messageBytes, privateKeyBytes);
return signatureBytes;
};
} 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)
}
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;
}
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')
var outputBuf = Buffer.concat([untrustedComment, sigInfoBase64, trustedComment, globalSigBase64])
return {
ed.sign = function(hash, privateKey) {
if (!(hash instanceof Buffer)) {
throw new Error('argument message must be a buffer');
}
const signature = Buffer.alloc(sodium.crypto_sign_BYTES);
sodium.crypto_sign_detached(signature, hash, privateKey);
return signature;
};
exports.sign = function (message, secretKey) {
var signature = new Buffer(sodium.crypto_sign_BYTES)
sodium.crypto_sign_detached(signature, message, secretKey)
return signature
}