Skip to content

Commit a71462e

Browse files
committed
Remove nacl.util. Closes #88, #44, #84.
Throw error if old nacl.util functions are used. nacl.util moved into a separate package: https://github.com/dchest/tweetnacl-util-js
1 parent 8b5630c commit a71462e

21 files changed

+40
-142
lines changed

README.md

+2-26
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ Documentation
2626
* [Hashing](#hashing)
2727
* [Random bytes generation](#random-bytes-generation)
2828
* [Constant-time comparison](#constant-time-comparison)
29-
* [Utilities](#utilities)
3029
* [Examples](#examples)
3130
* [System requirements](#system-requirements)
3231
* [Development and testing](#development-and-testing)
@@ -70,7 +69,8 @@ Usage
7069
------
7170

7271
All API functions accept and return bytes as `Uint8Array`s. If you need to
73-
encode or decode strings, use functions from `nacl.util` namespace.
72+
encode or decode strings, use functions from <https://github.com/dchest/tweetnacl-util-js>
73+
or one of the more robust codec packages.
7474

7575
### Public-key authenticated encryption (box)
7676

@@ -326,30 +326,6 @@ Returns `false` if either of the arguments has zero length, or arguments have
326326
different lengths, or their contents differ.
327327

328328

329-
### Utilities
330-
331-
Encoding/decoding functions are provided for convenience. They are correct,
332-
however their performance and wide compatibility with uncommon runtimes is not
333-
something that is considered important compared to the simplicity and size of
334-
implementation. You can use third-party libraries if you need to.
335-
336-
#### nacl.util.decodeUTF8(string)
337-
338-
Decodes string and returns `Uint8Array` of bytes.
339-
340-
#### nacl.util.encodeUTF8(array)
341-
342-
Encodes `Uint8Array` or `Array` of bytes into string.
343-
344-
#### nacl.util.decodeBase64(string)
345-
346-
Decodes Base-64 encoded string and returns `Uint8Array` of bytes.
347-
348-
#### nacl.util.encodeBase64(array)
349-
350-
Encodes `Uint8Array` or `Array` of bytes into string using Base-64 encoding.
351-
352-
353329
System requirements
354330
-------------------
355331

nacl-fast.js

+7-33
Original file line numberDiff line numberDiff line change
@@ -2157,39 +2157,13 @@ function cleanup(arr) {
21572157
for (var i = 0; i < arr.length; i++) arr[i] = 0;
21582158
}
21592159

2160-
nacl.util = {};
2161-
2162-
nacl.util.decodeUTF8 = function(s) {
2163-
var i, d = unescape(encodeURIComponent(s)), b = new Uint8Array(d.length);
2164-
for (i = 0; i < d.length; i++) b[i] = d.charCodeAt(i);
2165-
return b;
2166-
};
2167-
2168-
nacl.util.encodeUTF8 = function(arr) {
2169-
var i, s = [];
2170-
for (i = 0; i < arr.length; i++) s.push(String.fromCharCode(arr[i]));
2171-
return decodeURIComponent(escape(s.join('')));
2172-
};
2173-
2174-
nacl.util.encodeBase64 = function(arr) {
2175-
if (typeof btoa === 'undefined') {
2176-
return (new Buffer(arr)).toString('base64');
2177-
} else {
2178-
var i, s = [], len = arr.length;
2179-
for (i = 0; i < len; i++) s.push(String.fromCharCode(arr[i]));
2180-
return btoa(s.join(''));
2181-
}
2182-
};
2183-
2184-
nacl.util.decodeBase64 = function(s) {
2185-
if (typeof atob === 'undefined') {
2186-
return new Uint8Array(Array.prototype.slice.call(new Buffer(s, 'base64'), 0));
2187-
} else {
2188-
var i, d = atob(s), b = new Uint8Array(d.length);
2189-
for (i = 0; i < d.length; i++) b[i] = d.charCodeAt(i);
2190-
return b;
2191-
}
2192-
};
2160+
// TODO: Completely remove this in v0.15.
2161+
if (!nacl.util) {
2162+
nacl.util = {};
2163+
nacl.util.decodeUTF8 = nacl.util.encodeUTF8 = nacl.util.encodeBase64 = nacl.util.decodeBase64 = function() {
2164+
throw new Error('nacl.util moved into separate package: https://github.com/dchest/tweetnacl-util-js');
2165+
};
2166+
}
21932167

21942168
nacl.randomBytes = function(n) {
21952169
var b = new Uint8Array(n);

nacl.js

+7-33
Original file line numberDiff line numberDiff line change
@@ -944,39 +944,13 @@ function cleanup(arr) {
944944
for (var i = 0; i < arr.length; i++) arr[i] = 0;
945945
}
946946

947-
nacl.util = {};
948-
949-
nacl.util.decodeUTF8 = function(s) {
950-
var i, d = unescape(encodeURIComponent(s)), b = new Uint8Array(d.length);
951-
for (i = 0; i < d.length; i++) b[i] = d.charCodeAt(i);
952-
return b;
953-
};
954-
955-
nacl.util.encodeUTF8 = function(arr) {
956-
var i, s = [];
957-
for (i = 0; i < arr.length; i++) s.push(String.fromCharCode(arr[i]));
958-
return decodeURIComponent(escape(s.join('')));
959-
};
960-
961-
nacl.util.encodeBase64 = function(arr) {
962-
if (typeof btoa === 'undefined') {
963-
return (new Buffer(arr)).toString('base64');
964-
} else {
965-
var i, s = [], len = arr.length;
966-
for (i = 0; i < len; i++) s.push(String.fromCharCode(arr[i]));
967-
return btoa(s.join(''));
968-
}
969-
};
970-
971-
nacl.util.decodeBase64 = function(s) {
972-
if (typeof atob === 'undefined') {
973-
return new Uint8Array(Array.prototype.slice.call(new Buffer(s, 'base64'), 0));
974-
} else {
975-
var i, d = atob(s), b = new Uint8Array(d.length);
976-
for (i = 0; i < d.length; i++) b[i] = d.charCodeAt(i);
977-
return b;
978-
}
979-
};
947+
// TODO: Completely remove this in v0.15.
948+
if (!nacl.util) {
949+
nacl.util = {};
950+
nacl.util.decodeUTF8 = nacl.util.encodeUTF8 = nacl.util.encodeBase64 = nacl.util.decodeBase64 = function() {
951+
throw new Error('nacl.util moved into separate package: https://github.com/dchest/tweetnacl-util-js');
952+
};
953+
}
980954

981955
nacl.randomBytes = function(n) {
982956
var b = new Uint8Array(n);

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
"tap-browser-color": "^0.1.2",
6262
"tape": "^4.0.0",
6363
"testling": "^1.7.1",
64+
"tweetnacl-util": "^0.13.3",
6465
"uglify-js": "^2.4.21"
6566
},
6667
"browser": {

test/00-utils.js

-15
This file was deleted.

test/00-utils.quick.js

-26
This file was deleted.

test/02-randombytes.quick.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var nacl = (typeof window !== 'undefined') ? window.nacl : require('../' + (process.env.NACL_SRC || 'nacl.min.js'));
2+
nacl.util = require('tweetnacl-util');
23
var test = require('tape');
34

45
test('nacl.randomBytes', function(t) {

test/03-onetimeauth.quick.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var nacl = (typeof window !== 'undefined') ? window.nacl : require('../' + (process.env.NACL_SRC || 'nacl.min.js'));
2+
nacl.util = require('tweetnacl-util');
23
var test = require('tape');
34

45
var specVectors = require('./data/onetimeauth.spec');
@@ -13,4 +14,3 @@ test('nacl.lowlevel.crypto_onetimeauth specified vectors', function(t) {
1314
});
1415
t.end();
1516
});
16-

test/04-secretbox.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var nacl = (typeof window !== 'undefined') ? window.nacl : require('../' + (process.env.NACL_SRC || 'nacl.min.js'));
2+
nacl.util = require('tweetnacl-util');
23
var test = require('tape');
34

45
var randomVectors = require('./data/secretbox.random');
@@ -21,4 +22,3 @@ test('nacl.secretbox random test vectors', function(t) {
2122
});
2223
t.end();
2324
});
24-

test/04-secretbox.quick.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var nacl = (typeof window !== 'undefined') ? window.nacl : require('../' + (process.env.NACL_SRC || 'nacl.min.js'));
2+
nacl.util = require('tweetnacl-util');
23
var test = require('tape');
34

45
var enc = nacl.util.encodeBase64;

test/05-scalarmult.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var nacl = (typeof window !== 'undefined') ? window.nacl : require('../' + (process.env.NACL_SRC || 'nacl.min.js'));
2+
nacl.util = require('tweetnacl-util');
23
var test = require('tape');
34

45
var randomVectors = require('./data/scalarmult.random');
@@ -40,4 +41,3 @@ test('nacl.scalarMult and nacl.scalarMult.base random test vectors', function(t)
4041
});
4142
t.end();
4243
});
43-

test/06-box.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var nacl = (typeof window !== 'undefined') ? window.nacl : require('../' + (process.env.NACL_SRC || 'nacl.min.js'));
2+
nacl.util = require('tweetnacl-util');
23
var test = require('tape');
34

45
var randomVectors = require('./data/box.random');

test/06-box.quick.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var nacl = (typeof window !== 'undefined') ? window.nacl : require('../' + (process.env.NACL_SRC || 'nacl.min.js'));
2+
nacl.util = require('tweetnacl-util');
23
var test = require('tape');
34

45
var enc = nacl.util.encodeBase64;

test/07-hash.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var nacl = (typeof window !== 'undefined') ? window.nacl : require('../' + (process.env.NACL_SRC || 'nacl.min.js'));
2+
nacl.util = require('tweetnacl-util');
23
var test = require('tape');
34

45
var randomVectors = require('./data/hash.random');

test/07-hash.quick.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var nacl = (typeof window !== 'undefined') ? window.nacl : require('../' + (process.env.NACL_SRC || 'nacl.min.js'));
2+
nacl.util = require('tweetnacl-util');
23
var test = require('tape');
34

45
var specVectors = require('./data/hash.spec');

test/08-sign.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var nacl = (typeof window !== 'undefined') ? window.nacl : require('../' + (process.env.NACL_SRC || 'nacl.min.js'));
2+
nacl.util = require('tweetnacl-util');
23
var test = require('tape');
34

45
var specVectors = require('./data/sign.spec');

test/08-sign.quick.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var nacl = (typeof window !== 'undefined') ? window.nacl : require('../' + (process.env.NACL_SRC || 'nacl.min.js'));
2+
nacl.util = require('tweetnacl-util');
23
var test = require('tape');
34

45
var enc = nacl.util.encodeBase64;

test/benchmark/bench.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ var log = helpers.log;
44

55
if (!nacl) throw new Error('nacl not loaded');
66

7+
function decodeUTF8(s) {
8+
var i, d = unescape(encodeURIComponent(s)), b = new Uint8Array(d.length);
9+
for (i = 0; i < d.length; i++) b[i] = d.charCodeAt(i);
10+
return b;
11+
}
12+
713
function benchmark(fn, bytes, num) {
814
if (!num) num = 1000;
915
var i, elapsed, start = new Date();
@@ -114,8 +120,8 @@ function box_seal_open_benchmark() {
114120
pk2 = new Uint8Array(32), sk2 = new Uint8Array(32);
115121
nacl.lowlevel.crypto_box_keypair(pk1, sk1);
116122
nacl.lowlevel.crypto_box_keypair(pk2, sk2);
117-
var nonce = nacl.util.decodeUTF8('123456789012345678901234');
118-
var msg = nacl.util.decodeUTF8((new Array(1024)).join('a'));
123+
var nonce = decodeUTF8('123456789012345678901234');
124+
var msg = decodeUTF8((new Array(1024)).join('a'));
119125
var box = null;
120126
log.start('Benchmarking box');
121127
benchmark(function() {
@@ -131,7 +137,7 @@ function sign_open_benchmark() {
131137
var k = nacl.sign.keyPair();
132138
var sk = k.secretKey;
133139
var pk = k.publicKey;
134-
var msg = nacl.util.decodeUTF8((new Array(128)).join('a'));
140+
var msg = decodeUTF8((new Array(128)).join('a'));
135141
var sm;
136142
log.start('Benchmarking sign');
137143
benchmark(function() {

test/c/00-secretbox.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
var nacl = require('../../' + (process.env.NACL_SRC || 'nacl.min.js'));
2-
var crypto = require('crypto');
2+
nacl.util = require('tweetnacl-util');
33
var spawn = require('child_process').spawn;
44
var path = require('path');
55
var test = require('tape');

test/c/04-sign.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
var nacl = require('../../' + (process.env.NACL_SRC || 'nacl.min.js'));
2-
var crypto = require('crypto');
2+
nacl.util = require('tweetnacl-util');
33
var spawn = require('child_process').spawn;
44
var path = require('path');
55
var test = require('tape');

test/c/05-sign-keypair.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
var nacl = require('../../' + (process.env.NACL_SRC || 'nacl.min.js'));
2-
var crypto = require('crypto');
2+
nacl.util = require('tweetnacl-util');
33
var spawn = require('child_process').spawn;
44
var execFile = require('child_process').execFile;
55
var path = require('path');

0 commit comments

Comments
 (0)