Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 28 additions & 6 deletions problems/09.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,45 @@
// sign.js
var sodium = require('sodium-native')

var args = process.argv

if (args.length !== 3) {
console.log(`Usage: node sign.js "<message>"`)
process.exit()
}

var message = Buffer.from(args[2])

var publicKey = Buffer.alloc(sodium.crypto_sign_PUBLICKEYBYTES)
var secretKey = Buffer.alloc(sodium.crypto_sign_SECRETKEYBYTES)
sodium.crypto_sign_keypair(publicKey, secretKey)

var message = Buffer.from('Hello world!')
var signature = Buffer.alloc(sodium.crypto_sign_BYTES)
sodium.crypto_sign_detached(signature, message, secretKey)

sodium.crypto_sign_detached(signature, message, privateKey)

console.log('Public key:': publicKey.toString('hex'))
console.log('Message:': message.toString())
console.log('Signature:': signature.toString('hex'))
console.log(`Message: ${message}`)
console.log(`Public key: ${publicKey.toString('hex')}`)
console.log(`Signature: ${signature.toString('hex')}`)
```

```js
// verify.js
var sodium = require('sodium-native')

var args = process.argv

if (args.length !== 5) {
console.log(`Usage: node verify.js "<message>" <publicKey (hex)> <signature (hex)>`)
process.exit()
}

var message = Buffer.from(args[2])
var publicKey = Buffer.from(args[3], 'hex')
var signature = Buffer.from(args[4], 'hex')

const isSignatureValid = sodium.crypto_sign_verify_detached(signature, message, publicKey)

console.log(`Signature is ${isSignatureValid ? 'valid' : 'invalid'}.`)
```

</details>
Expand Down