-
Notifications
You must be signed in to change notification settings - Fork 1
/
sign.js
40 lines (34 loc) · 1.06 KB
/
sign.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
'use strict'
const isBuffer = require('is-buffer')
const alloc = require('buffer-alloc-unsafe')
const {
crypto_sign_detached,
crypto_sign_BYTES,
} = require('sodium-universal')
/**
* Sign a message buffer with a secret key buffer. This function calls
* `crypto_sign_detached` on a buffer of size `crypto_sign_BYTES`.
* @public
* @param {Buffer} message
* @param {Buffer} secretKey
* @return {Buffer}
* @throws TypeError
*/
function sign(message, secretKey) {
if (null == message || false == isBuffer(message)) {
throw new TypeError("sign: Expecting message to be a buffer.")
} else if (0 == message.length) {
throw new TypeError("sign: Cannot sign an empty message.")
}
if (null == secretKey || false == isBuffer(secretKey)) {
throw new TypeError("sign: Expecting secretKey to be a buffer.")
} else if (0 == secretKey.length) {
throw new TypeError("sign: Cannot sign with an empty secretKey.")
}
const buffer = alloc(crypto_sign_BYTES)
crypto_sign_detached(buffer, message, secretKey)
return buffer
}
module.exports = {
sign
}