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/minisign.pub', (err, PK) => {
t.error(err)
var PKinfo = minisign.parsePubKey(PK)
t.ok(minisign.verifySignature(parsedOutput1, contentToSign, PKinfo))
t.ok(sodium.crypto_sign_verify_detached(parsedOutput2.signature, contentToSign, PKinfo.publicKey))
t.ok(minisign.verifySignature(parsedOutput2, contentToSign, PKinfo))
t.end()
})
})
if (signature.signatureAlgorithm.toString() === 'ED') {
var hashedContent = Buffer.alloc(sodium.crypto_generichash_BYTES_MAX)
sodium.crypto_generichash(hashedContent, originalContent)
contentSigned = hashedContent
} else {
contentSigned = originalContent
}
if (!(signature.keyID.equals(publicKeyInfo.keyID))) {
return ("error: keyID's do not match")
} else {
if (!(sodium.crypto_sign_verify_detached(signature.signature, contentSigned, publicKeyInfo.publicKey))) {
return ('error: signature verification failed')
} else {
var forGlobalSig = Buffer.concat([signature.signature, Buffer.from(signature.trustedComment)])
if (!(sodium.crypto_sign_verify_detached(signature.globalSignature, forGlobalSig, publicKeyInfo.publicKey))) {
return ('error: trusted comment cannot be verified')
}
}
}
return ('signature and comment successfully verified')
}
function verifySignature (signedContent, originalContent, publicKeyInfo) {
var contentSigned
var signature = parseSignature(signedContent)
if (signature.signatureAlgorithm.toString() === 'ED') {
var hashedContent = Buffer.alloc(sodium.crypto_generichash_BYTES_MAX)
sodium.crypto_generichash(hashedContent, originalContent)
contentSigned = hashedContent
} else {
contentSigned = originalContent
}
if (!(signature.keyID.equals(publicKeyInfo.keyID))) {
return ("error: keyID's do not match")
} else {
if (!(sodium.crypto_sign_verify_detached(signature.signature, contentSigned, publicKeyInfo.publicKey))) {
return ('error: signature verification failed')
} else {
var forGlobalSig = Buffer.concat([signature.signature, Buffer.from(signature.trustedComment)])
if (!(sodium.crypto_sign_verify_detached(signature.globalSignature, forGlobalSig, publicKeyInfo.publicKey))) {
return ('error: trusted comment cannot be verified')
}
}
}
return ('signature and comment successfully verified')
}
export function verify(
publicKey: EncodedPublicSigningKey,
message: Buffer,
signature: EncodedSignature
): boolean {
const publicKeyBuffer = decode(publicKey)
const signatureBuffer = decode(signature)
return sodium.crypto_sign_verify_detached(signatureBuffer, message, publicKeyBuffer)
}
if (signature.signatureAlgorithm.equals(Buffer.from('ED'))) {
var hashedContent = Buffer.alloc(sodium.crypto_generichash_BYTES_MAX)
sodium.crypto_generichash(hashedContent, originalContent)
contentSigned = hashedContent
} else {
contentSigned = originalContent
}
if (!(signature.keyID.equals(publicKeyInfo.keyID))) {
throw new Error("keyID's do not match")
} else {
if (!(sodium.crypto_sign_verify_detached(signature.signature, contentSigned, publicKeyInfo.publicKey))) {
throw new Error('signature verification failed')
} else {
var forGlobalSig = Buffer.concat([signature.signature, Buffer.from(signature.trustedComment)])
if (!(sodium.crypto_sign_verify_detached(signature.globalSignature, forGlobalSig, publicKeyInfo.publicKey))) {
throw new Error('trusted comment cannot be verified')
}
}
}
return true
}
exports.verify = function signVerify (signature, message, publicKey) {
if (!(signature instanceof Buffer)) {
throw new Error('signature must be a Buffer')
}
if (!(message instanceof Buffer)) {
throw new Error('message must be a Buffer')
}
if (!(publicKey instanceof Buffer)) {
throw new Error('publicKey must be a Buffer')
}
return sodium.crypto_sign_verify_detached(signature, message, publicKey)
}
function verifySignature (signature, originalContent, publicKeyInfo) {
var contentSigned
if (signature.signatureAlgorithm.equals(Buffer.from('ED'))) {
var hashedContent = Buffer.alloc(sodium.crypto_generichash_BYTES_MAX)
sodium.crypto_generichash(hashedContent, originalContent)
contentSigned = hashedContent
} else {
contentSigned = originalContent
}
if (!(signature.keyID.equals(publicKeyInfo.keyID))) {
throw new Error("keyID's do not match")
} else {
if (!(sodium.crypto_sign_verify_detached(signature.signature, contentSigned, publicKeyInfo.publicKey))) {
throw new Error('signature verification failed')
} else {
var forGlobalSig = Buffer.concat([signature.signature, Buffer.from(signature.trustedComment)])
if (!(sodium.crypto_sign_verify_detached(signature.globalSignature, forGlobalSig, publicKeyInfo.publicKey))) {
throw new Error('trusted comment cannot be verified')
}
}
}
return true
}
exports.verify = function (message, signature, publicKey) {
return sodium.crypto_sign_verify_detached(signature, message, publicKey)
}
export const verifyDetached: NaclInterface['verifyDetached'] = (
messageBytes,
signatureBytes,
publicKeyBytes,
) =>
sodium.crypto_sign_verify_detached(
signatureBytes,
messageBytes,
publicKeyBytes,
);