Skip to content
This repository was archived by the owner on Jul 3, 2020. It is now read-only.

Commit 639bf2d

Browse files
author
Face Kapow
committed
Allow key to be specified or generated for crypto_auth
1 parent 377b3df commit 639bf2d

1 file changed

Lines changed: 11 additions & 7 deletions

File tree

js/core/libsodium.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ function hexToU8(hexStr) {
2626
return u8;
2727
}
2828

29-
function stringOrU8(arg, funcName, argName, i) {
29+
function stringOrU8(arg, funcName, argName, i, msg) {
3030
var ret = arg;
3131
if (!(arg instanceof Uint8Array)) {
3232
if (typeof arg === 'string') {
@@ -35,7 +35,7 @@ function stringOrU8(arg, funcName, argName, i) {
3535
ret[i] = arg.charCodeAt(i);
3636
}
3737
} else {
38-
throw new Error(funcName + ': ' + argName + '(argument ' + i + ') must be a string or Uint8Array.');
38+
throw new Error(funcName + ': ' + argName + '(argument ' + i + ') ' + (msg ? msg : 'must be a string or Uint8Array') + '.');
3939
}
4040
}
4141
return ret;
@@ -68,18 +68,22 @@ module.exports = {
6868
}
6969
return hexToU8(decipher);
7070
},
71-
crypto_auth: function(data) {
71+
crypto_auth: function(data, key) {
7272
var dataArr = stringOrU8(data, 'crypto_auth', 'data', 0);
73+
var keyArr;
74+
if (key === null || key === undefined) {
75+
keyArr = runtime.random.getRandomValues(constants.crypto_auth_KEYBYTES);
76+
} else {
77+
keyArr = stringOrU8(key, 'crypto_auth', 'key', 1, 'must be unspecified, a string, or Uint8Array');
78+
}
7379

74-
var key = runtime.random.getRandomValues(constants.crypto_auth_KEYBYTES);
75-
76-
var mac = lib.crypto_auth(dataArr, key);
80+
var mac = lib.crypto_auth(dataArr, keyArr);
7781
if (!mac) {
7882
throw new Error('crypto_auth: error creating tag.');
7983
}
8084
return {
8185
mac: hexToU8(mac),
82-
key: key
86+
key: keyArr
8387
};
8488
},
8589
crypto_auth_verify: function(mac, key, data) {

0 commit comments

Comments
 (0)