fastbcrypt 0.0.0-stage → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/node_blf.h ADDED
@@ -0,0 +1,132 @@
1
+ /* $OpenBSD: blf.h,v 1.7 2007/03/14 17:59:41 grunk Exp $ */
2
+ /*
3
+ * Blowfish - a fast block cipher designed by Bruce Schneier
4
+ *
5
+ * Copyright 1997 Niels Provos <provos@physnet.uni-hamburg.de>
6
+ * All rights reserved.
7
+ *
8
+ * Redistribution and use in source and binary forms, with or without
9
+ * modification, are permitted provided that the following conditions
10
+ * are met:
11
+ * 1. Redistributions of source code must retain the above copyright
12
+ * notice, this list of conditions and the following disclaimer.
13
+ * 2. Redistributions in binary form must reproduce the above copyright
14
+ * notice, this list of conditions and the following disclaimer in the
15
+ * documentation and/or other materials provided with the distribution.
16
+ * 3. All advertising materials mentioning features or use of this software
17
+ * must display the following acknowledgement:
18
+ * This product includes software developed by Niels Provos.
19
+ * 4. The name of the author may not be used to endorse or promote products
20
+ * derived from this software without specific prior written permission.
21
+ *
22
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
+ */
33
+
34
+ #ifndef _NODE_BLF_H_
35
+ #define _NODE_BLF_H_
36
+
37
+ #include <sys/types.h>
38
+
39
+ /* Solaris compatibility */
40
+ #ifdef __sun
41
+ #define u_int8_t uint8_t
42
+ #define u_int16_t uint16_t
43
+ #define u_int32_t uint32_t
44
+ #define u_int64_t uint64_t
45
+ #endif
46
+
47
+ #ifdef _WIN32
48
+ #define u_int8_t unsigned __int8
49
+ #define u_int16_t unsigned __int16
50
+ #define u_int32_t unsigned __int32
51
+ #define u_int64_t unsigned __int64
52
+ #endif
53
+
54
+ /* Windows ssize_t compatibility */
55
+ #if defined(_WIN32) || defined(_WIN64)
56
+ # if defined(_WIN64)
57
+ typedef __int64 LONG_PTR;
58
+ # else
59
+ typedef long LONG_PTR;
60
+ # endif
61
+ typedef LONG_PTR SSIZE_T;
62
+ typedef SSIZE_T ssize_t;
63
+ #endif
64
+
65
+ /* z/OS compatibility */
66
+ #ifdef __MVS__
67
+ typedef unsigned char u_int8_t;
68
+ typedef unsigned short u_int16_t;
69
+ typedef unsigned int u_int32_t;
70
+ typedef unsigned long long u_int64_t;
71
+ #endif
72
+
73
+ #define BCRYPT_VERSION '2'
74
+ #define BCRYPT_MAXSALT 16 /* Precomputation is just so nice */
75
+ #define BCRYPT_BLOCKS 6 /* Ciphertext blocks */
76
+ #define BCRYPT_MINROUNDS 16 /* we have log2(rounds) in salt */
77
+
78
+ /* Schneier specifies a maximum key length of 56 bytes.
79
+ * This ensures that every key bit affects every cipher
80
+ * bit. However, the subkeys can hold up to 72 bytes.
81
+ * Warning: For normal blowfish encryption only 56 bytes
82
+ * of the key affect all cipherbits.
83
+ */
84
+
85
+ #define BLF_N 16 /* Number of Subkeys */
86
+ #define BLF_MAXKEYLEN ((BLF_N-2)*4) /* 448 bits */
87
+ #define BLF_MAXUTILIZED ((BLF_N+2)*4) /* 576 bits */
88
+
89
+ #define _PASSWORD_LEN 128 /* max length, not counting NUL */
90
+ #define _SALT_LEN 32 /* max length */
91
+
92
+ /* Blowfish context */
93
+ typedef struct BlowfishContext {
94
+ u_int32_t S[4][256]; /* S-Boxes */
95
+ u_int32_t P[BLF_N + 2]; /* Subkeys */
96
+ } blf_ctx;
97
+
98
+ /* Raw access to customized Blowfish
99
+ * blf_key is just:
100
+ * Blowfish_initstate( state )
101
+ * Blowfish_expand0state( state, key, keylen )
102
+ */
103
+
104
+ void Blowfish_encipher(blf_ctx *, u_int32_t *, u_int32_t *);
105
+ void Blowfish_decipher(blf_ctx *, u_int32_t *, u_int32_t *);
106
+ void Blowfish_initstate(blf_ctx *);
107
+ void Blowfish_expand0state(blf_ctx *, const u_int8_t *, u_int16_t);
108
+ void Blowfish_expandstate
109
+ (blf_ctx *, const u_int8_t *, u_int16_t, const u_int8_t *, u_int16_t);
110
+
111
+ /* Standard Blowfish */
112
+
113
+ void blf_key(blf_ctx *, const u_int8_t *, u_int16_t);
114
+ void blf_enc(blf_ctx *, u_int32_t *, u_int16_t);
115
+ void blf_dec(blf_ctx *, u_int32_t *, u_int16_t);
116
+
117
+ void blf_ecb_encrypt(blf_ctx *, u_int8_t *, u_int32_t);
118
+ void blf_ecb_decrypt(blf_ctx *, u_int8_t *, u_int32_t);
119
+
120
+ void blf_cbc_encrypt(blf_ctx *, u_int8_t *, u_int8_t *, u_int32_t);
121
+ void blf_cbc_decrypt(blf_ctx *, u_int8_t *, u_int8_t *, u_int32_t);
122
+
123
+ /* Converts u_int8_t to u_int32_t */
124
+ u_int32_t Blowfish_stream2word(const u_int8_t *, u_int16_t , u_int16_t *);
125
+
126
+ /* bcrypt functions*/
127
+ void bcrypt_gensalt(char, u_int8_t, u_int8_t*, char *);
128
+ void bcrypt(const char *, size_t key_len, const char *, char *);
129
+ void encode_salt(char *, u_int8_t *, char, u_int16_t, u_int8_t);
130
+ u_int32_t bcrypt_get_rounds(const char *);
131
+
132
+ #endif
@@ -0,0 +1,209 @@
1
+ const fastbcrypt = require('..');
2
+
3
+ test('salt_length', done => {
4
+ expect.assertions(1);
5
+ fastbcrypt.genSalt(10, function (err, salt) {
6
+ expect(salt).toHaveLength(29);
7
+ done();
8
+ });
9
+ })
10
+
11
+ test('salt_only_cb', () => {
12
+ expect.assertions(1);
13
+ expect(() => {
14
+ fastbcrypt.genSalt((err, salt) => {
15
+ });
16
+ }).not.toThrow();
17
+ })
18
+
19
+ test('salt_rounds_is_string_number', done => {
20
+ expect.assertions(2);
21
+ fastbcrypt.genSalt('10', void 0, function (err, salt) {
22
+ expect(err instanceof Error).toBe(true)
23
+ expect(err.message).toBe('rounds must be a number')
24
+ done();
25
+ });
26
+ })
27
+
28
+ test('salt_rounds_is_string_non_number', done => {
29
+ expect.assertions(2);
30
+ fastbcrypt.genSalt('z', function (err, salt) {
31
+ expect(err instanceof Error).toBe(true)
32
+ expect(err.message).toBe('rounds must be a number')
33
+ done();
34
+ });
35
+ })
36
+
37
+ test('salt_minor', done => {
38
+ expect.assertions(3);
39
+ fastbcrypt.genSalt(10, 'a', function (err, value) {
40
+ expect(value).toHaveLength(29);
41
+ const [_, minor, salt] = value.split('$');
42
+ expect(minor).toEqual('2a');
43
+ expect(salt).toEqual('10');
44
+ done();
45
+ });
46
+ })
47
+
48
+ test('salt_minor_b', done => {
49
+ expect.assertions(3);
50
+ fastbcrypt.genSalt(10, 'b', function (err, value) {
51
+ expect(value).toHaveLength(29);
52
+ const [_, minor, salt] = value.split('$');
53
+ expect(minor).toEqual('2b');
54
+ expect(salt).toEqual('10');
55
+ done();
56
+ });
57
+ })
58
+
59
+ test('hash', done => {
60
+ expect.assertions(2);
61
+ fastbcrypt.genSalt(10, function (err, salt) {
62
+ fastbcrypt.hash('password', salt, function (err, res) {
63
+ expect(res).toBeDefined();
64
+ expect(err).toBeUndefined();
65
+ done();
66
+ });
67
+ });
68
+ })
69
+
70
+ test('hash_rounds', done => {
71
+ expect.assertions(1);
72
+ fastbcrypt.hash('bacon', 8, function (err, hash) {
73
+ expect(fastbcrypt.getRounds(hash)).toEqual(8);
74
+ done();
75
+ });
76
+ })
77
+
78
+ test('hash_empty_strings', done => {
79
+ expect.assertions(1);
80
+ fastbcrypt.genSalt(10, function (err, salt) {
81
+ fastbcrypt.hash('', salt, function (err, res) {
82
+ expect(res).toBeDefined();
83
+ done();
84
+ });
85
+ });
86
+ })
87
+
88
+ test('hash_fails_with_empty_salt', done => {
89
+ expect.assertions(1);
90
+ fastbcrypt.hash('', '', function (err, res) {
91
+ expect(err.message).toBe('Invalid salt. Salt must be in the form of: $Vers$log2(NumRounds)$saltvalue')
92
+ done();
93
+ });
94
+ })
95
+
96
+ test('hash_no_params', done => {
97
+ expect.assertions(1);
98
+ fastbcrypt.hash(function (err, hash) {
99
+ expect(err.message).toBe('data must be a string or Buffer and salt must either be a salt string or a number of rounds')
100
+ done();
101
+ });
102
+ })
103
+
104
+ test('hash_one_param', done => {
105
+ expect.assertions(1);
106
+ fastbcrypt.hash('password', function (err, hash) {
107
+ expect(err.message).toBe('data must be a string or Buffer and salt must either be a salt string or a number of rounds');
108
+ done();
109
+ });
110
+ })
111
+
112
+ test('hash_salt_validity', done => {
113
+ expect.assertions(2);
114
+ fastbcrypt.hash('password', '$2a$10$somesaltyvaluertsetrse', function (err, enc) {
115
+ expect(err).toBeUndefined();
116
+ fastbcrypt.hash('password', 'some$value', function (err, enc) {
117
+ expect(err.message).toBe("Invalid salt. Salt must be in the form of: $Vers$log2(NumRounds)$saltvalue");
118
+ done();
119
+ });
120
+ });
121
+ })
122
+
123
+ test('verify_salt', done => {
124
+ expect.assertions(2);
125
+ fastbcrypt.genSalt(10, function (err, value) {
126
+ const [_, version, rounds] = value.split('$');
127
+ expect(version).toEqual('2b');
128
+ expect(rounds).toEqual('10');
129
+ done();
130
+ });
131
+ })
132
+
133
+ test('verify_salt_min_rounds', done => {
134
+ expect.assertions(2);
135
+ fastbcrypt.genSalt(1, function (err, value) {
136
+ const [_, version, rounds] = value.split('$');
137
+ expect(version).toEqual('2b');
138
+ expect(rounds).toEqual('04');
139
+ done();
140
+ });
141
+ })
142
+
143
+ test('verify_salt_max_rounds', done => {
144
+ expect.assertions(2);
145
+ fastbcrypt.genSalt(100, function (err, value) {
146
+ const [_, version, rounds] = value.split('$');
147
+ expect(version).toEqual('2b');
148
+ expect(rounds).toEqual('31');
149
+ done();
150
+ });
151
+ })
152
+
153
+ test('hash_compare', done => {
154
+ expect.assertions(2);
155
+ fastbcrypt.genSalt(10, function (err, salt) {
156
+ fastbcrypt.hash("test", salt, function (err, hash) {
157
+ fastbcrypt.compare("test", hash, function (err, res) {
158
+ expect(hash).toBeDefined();
159
+ fastbcrypt.compare("blah", hash, function (err, res) {
160
+ expect(res).toBe(false);
161
+ done();
162
+ });
163
+ });
164
+ });
165
+ });
166
+ })
167
+
168
+ test('hash_compare_empty_strings', done => {
169
+ expect.assertions(2);
170
+ const hash = fastbcrypt.hashSync("test", fastbcrypt.genSaltSync(10));
171
+
172
+ fastbcrypt.compare("", hash, function (err, res) {
173
+ expect(res).toEqual(false)
174
+ fastbcrypt.compare("", "", function (err, res) {
175
+ expect(res).toEqual(false);
176
+ done();
177
+ });
178
+ });
179
+ })
180
+
181
+ test('hash_compare_invalid_strings', done => {
182
+ expect.assertions(2);
183
+ const fullString = 'envy1362987212538';
184
+ const hash = '$2a$10$XOPbrlUPQdwdJUpSrIF6X.LbE14qsMmKGhM1A8W9iqaG3vv1BD7WC';
185
+ const wut = ':';
186
+ fastbcrypt.compare(fullString, hash, function (err, res) {
187
+ expect(res).toBe(true);
188
+ fastbcrypt.compare(fullString, wut, function (err, res) {
189
+ expect(res).toBe(false);
190
+ done();
191
+ });
192
+ });
193
+ })
194
+
195
+ test('compare_no_params', done => {
196
+ expect.assertions(1);
197
+ fastbcrypt.compare(function (err, hash) {
198
+ expect(err.message).toBe('data and hash arguments required');
199
+ done();
200
+ });
201
+ })
202
+
203
+ test('hash_compare_one_param', done => {
204
+ expect.assertions(1);
205
+ fastbcrypt.compare('password', function (err, hash) {
206
+ expect(err.message).toBe('data and hash arguments required');
207
+ done();
208
+ });
209
+ })
@@ -0,0 +1,48 @@
1
+ const fastbcrypt = require('..');
2
+
3
+ // some tests were adapted from https://github.com/riverrun/bcrypt_elixir/blob/master/test/base_test.exs
4
+ // which are under the BSD LICENSE
5
+
6
+ test('openwall', () => {
7
+ expect(fastbcrypt.hashSync("U*U", "$2a$05$CCCCCCCCCCCCCCCCCCCCC.")).toStrictEqual("$2a$05$CCCCCCCCCCCCCCCCCCCCC.E5YPO9kmyuRGyh0XouQYb4YMJKvyOeW");
8
+ expect(fastbcrypt.hashSync("U*U*", "$2a$05$CCCCCCCCCCCCCCCCCCCCC.")).toStrictEqual("$2a$05$CCCCCCCCCCCCCCCCCCCCC.VGOzA784oUp/Z0DY336zx7pLYAy0lwK");
9
+ expect(fastbcrypt.hashSync("U*U*U", "$2a$05$XXXXXXXXXXXXXXXXXXXXXO")).toStrictEqual("$2a$05$XXXXXXXXXXXXXXXXXXXXXOAcXxm9kjPGEMsLznoKqmqw7tc8WCx4a");
10
+ expect(fastbcrypt.hashSync("", "$2a$05$CCCCCCCCCCCCCCCCCCCCC.")).toStrictEqual("$2a$05$CCCCCCCCCCCCCCCCCCCCC.7uG0VCzI2bS7j6ymqJi9CdcdxiRTWNy");
11
+ expect(fastbcrypt.hashSync("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", "$2a$05$abcdefghijklmnopqrstuu")).toStrictEqual("$2a$05$abcdefghijklmnopqrstuu5s2v8.iXieOjg/.AySBTTZIIVFJeBui");
12
+ })
13
+
14
+ test('openbsd', () => {
15
+ expect(fastbcrypt.hashSync("000000000000000000000000000000000000000000000000000000000000000000000000", "$2a$05$CCCCCCCCCCCCCCCCCCCCC.")).toStrictEqual("$2a$05$CCCCCCCCCCCCCCCCCCCCC.6.O1dLNbjod2uo0DVcW.jHucKbPDdHS")
16
+ expect(fastbcrypt.hashSync("000000000000000000000000000000000000000000000000000000000000000000000000", "$2b$05$CCCCCCCCCCCCCCCCCCCCC.")).toStrictEqual("$2b$05$CCCCCCCCCCCCCCCCCCCCC.6.O1dLNbjod2uo0DVcW.jHucKbPDdHS")
17
+ })
18
+
19
+ test('long_passwords', () => {
20
+ // bcrypt wrap-around bug in $2a$
21
+ expect(fastbcrypt.hashSync("012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234", "$2a$05$CCCCCCCCCCCCCCCCCCCCC.")).toStrictEqual("$2a$05$CCCCCCCCCCCCCCCCCCCCC.6.O1dLNbjod2uo0DVcW.jHucKbPDdHS")
22
+ expect(fastbcrypt.hashSync("01XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", "$2a$05$CCCCCCCCCCCCCCCCCCCCC.")).toStrictEqual("$2a$05$CCCCCCCCCCCCCCCCCCCCC.6.O1dLNbjod2uo0DVcW.jHucKbPDdHS")
23
+
24
+ // tests for $2b$ which fixes wrap-around bugs
25
+ expect(fastbcrypt.hashSync("012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234", "$2b$05$CCCCCCCCCCCCCCCCCCCCC.")).toStrictEqual("$2b$05$CCCCCCCCCCCCCCCCCCCCC.XxrQqgBi/5Sxuq9soXzDtjIZ7w5pMfK")
26
+ expect(fastbcrypt.hashSync("0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345", "$2b$05$CCCCCCCCCCCCCCCCCCCCC.")).toStrictEqual("$2b$05$CCCCCCCCCCCCCCCCCCCCC.XxrQqgBi/5Sxuq9soXzDtjIZ7w5pMfK")
27
+ })
28
+
29
+ test('embedded_nulls', () => {
30
+ expect(fastbcrypt.hashSync("Passw\0rd123", "$2b$05$CCCCCCCCCCCCCCCCCCCCC.")).toStrictEqual("$2b$05$CCCCCCCCCCCCCCCCCCCCC.VHy/kzL4sCcX3Ib3wN5rNGiRt.TpfxS")
31
+ expect(fastbcrypt.hashSync("Passw\0 you can literally write anything after the NUL character", "$2b$05$CCCCCCCCCCCCCCCCCCCCC.")).toStrictEqual("$2b$05$CCCCCCCCCCCCCCCCCCCCC.4vJLJQ6nZ/70INTjjSZWQ0iyUek92tu")
32
+ expect(fastbcrypt.hashSync(Buffer.from("Passw\0 you can literally write anything after the NUL character"), "$2b$05$CCCCCCCCCCCCCCCCCCCCC.")).toStrictEqual("$2b$05$CCCCCCCCCCCCCCCCCCCCC.4vJLJQ6nZ/70INTjjSZWQ0iyUek92tu")
33
+ })
34
+
35
+ test('shorten_salt_to_128_bits', () => {
36
+ expect(fastbcrypt.hashSync("test", "$2a$10$1234567899123456789012")).toStrictEqual("$2a$10$123456789912345678901u.OtL1A1eGK5wmvBKUDYKvuVKI7h2XBu")
37
+ expect(fastbcrypt.hashSync("U*U*", "$2a$05$CCCCCCCCCCCCCCCCCCCCCh")).toStrictEqual("$2a$05$CCCCCCCCCCCCCCCCCCCCCeUQ7VjYZ2hd4bLYZdhuPpZMUpEUJDw1S")
38
+ expect(fastbcrypt.hashSync("U*U*", "$2a$05$CCCCCCCCCCCCCCCCCCCCCM")).toStrictEqual("$2a$05$CCCCCCCCCCCCCCCCCCCCC.VGOzA784oUp/Z0DY336zx7pLYAy0lwK")
39
+ expect(fastbcrypt.hashSync("U*U*", "$2a$05$CCCCCCCCCCCCCCCCCCCCCA")).toStrictEqual("$2a$05$CCCCCCCCCCCCCCCCCCCCC.VGOzA784oUp/Z0DY336zx7pLYAy0lwK")
40
+ })
41
+
42
+ test('consistency', () => {
43
+ expect(fastbcrypt.hashSync("ππππππππ", "$2a$10$.TtQJ4Jr6isd4Hp.mVfZeu")).toStrictEqual("$2a$10$.TtQJ4Jr6isd4Hp.mVfZeuh6Gws4rOQ/vdBczhDx.19NFK0Y84Dle")
44
+ expect(fastbcrypt.hashSync("p@5sw0rd", "$2b$12$zQ4CooEXdGqcwi0PHsgc8e")).toStrictEqual("$2b$12$zQ4CooEXdGqcwi0PHsgc8eAf0DLXE/XHoBE8kCSGQ97rXwuClaPam")
45
+ expect(fastbcrypt.hashSync("C'est bon, la vie!", "$2b$12$cbo7LZ.wxgW4yxAA5Vqlv.")).toStrictEqual("$2b$12$cbo7LZ.wxgW4yxAA5Vqlv.KR6QFPt4qCdc9RYJNXxa/rbUOp.1sw.")
46
+ expect(fastbcrypt.hashSync("ἓν οἶδα ὅτι οὐδὲν οἶδα", "$2b$12$LeHKWR2bmrazi/6P22Jpau")).toStrictEqual("$2b$12$LeHKWR2bmrazi/6P22JpauX5my/eKwwKpWqL7L5iEByBnxNc76FRW")
47
+ expect(fastbcrypt.hashSync(Buffer.from("ἓν οἶδα ὅτι οὐδὲν οἶδα"), "$2b$12$LeHKWR2bmrazi/6P22Jpau")).toStrictEqual("$2b$12$LeHKWR2bmrazi/6P22JpauX5my/eKwwKpWqL7L5iEByBnxNc76FRW")
48
+ })
@@ -0,0 +1,168 @@
1
+ const fastbcrypt = require('..');
2
+ const promises = require('../promises');
3
+
4
+ test('salt_returns_promise_on_no_args', () => {
5
+ // make sure test passes with non-native implementations such as bluebird
6
+ // http://stackoverflow.com/questions/27746304/how-do-i-tell-if-an-object-is-a-promise
7
+ expect(typeof fastbcrypt.genSalt().then).toEqual('function')
8
+ })
9
+
10
+ test('salt_returns_promise_on_null_callback', () => {
11
+ expect(typeof fastbcrypt.genSalt(13, null, null).then).toEqual('function')
12
+ })
13
+
14
+ test('salt_length', () => {
15
+ return expect(fastbcrypt.genSalt(10)).resolves.toHaveLength(29);
16
+ })
17
+
18
+ test('salt_rounds_is_string_number', () => {
19
+ return expect(fastbcrypt.genSalt('10')).rejects.toThrow('rounds must be a number');
20
+ })
21
+
22
+ test('salt_rounds_is_string_non_number', () => {
23
+ return expect(fastbcrypt.genSalt('b')).rejects.toThrow('rounds must be a number');
24
+ })
25
+
26
+ test('hash_returns_promise_on_null_callback', () => {
27
+ expect(typeof fastbcrypt.hash('password', 10, null).then).toStrictEqual('function')
28
+ })
29
+
30
+ test('hash', () => {
31
+ return expect(fastbcrypt.genSalt(10)
32
+ .then(salt => fastbcrypt.hash('password', salt))).resolves.toBeDefined()
33
+ })
34
+
35
+ test('hash_rounds', () => {
36
+ return fastbcrypt.hash('bacon', 8).then(hash => {
37
+ expect(fastbcrypt.getRounds(hash)).toStrictEqual(8)
38
+ });
39
+ })
40
+
41
+ test('hash_empty_strings', () => {
42
+ expect.assertions(2);
43
+ return Promise.all([
44
+ expect(fastbcrypt.genSalt(10)
45
+ .then(salt => fastbcrypt.hash('', salt)))
46
+ .resolves.toBeDefined(),
47
+ expect(fastbcrypt.hash('', '')).rejects.toThrow(''),
48
+ ]);
49
+ })
50
+
51
+ test('hash_no_params', () => {
52
+ expect.assertions(1);
53
+ return expect(fastbcrypt.hash()).rejects.toThrow('data and salt arguments required');
54
+ })
55
+
56
+ test('hash_one_param', () => {
57
+ return expect(fastbcrypt.hash('password')).rejects.toThrow('data and salt arguments required');
58
+ })
59
+
60
+ test('hash_salt_validity', () => {
61
+ expect.assertions(2);
62
+ return Promise.all(
63
+ [
64
+ expect(fastbcrypt.hash('password', '$2a$10$somesaltyvaluertsetrse')).resolves.toBeDefined(),
65
+ expect(fastbcrypt.hash('password', 'some$value')).rejects.toThrow("Invalid salt. Salt must be in the form of: $Vers$log2(NumRounds)$saltvalue")
66
+ ]);
67
+ })
68
+
69
+ test('verify_salt', () => {
70
+ expect.assertions(2);
71
+ return fastbcrypt.genSalt(10).then(result => {
72
+ const [_, version, salt] = result.split('$');
73
+ expect(version).toEqual('2b')
74
+ expect(salt).toEqual('10')
75
+ });
76
+ })
77
+
78
+ test('verify_salt_min_rounds', () => {
79
+ expect.assertions(2);
80
+ return fastbcrypt.genSalt(1).then(value => {
81
+ const [_, version, rounds] = value.split('$');
82
+ expect(version).toEqual('2b');
83
+ expect(rounds).toEqual('04');
84
+ });
85
+ })
86
+
87
+ test('verify_salt_max_rounds', () => {
88
+ expect.assertions(2);
89
+ return fastbcrypt.genSalt(100).then(value => {
90
+ const [_, version, rounds] = value.split('$');
91
+ expect(version).toEqual('2b');
92
+ expect(rounds).toEqual('31');
93
+ });
94
+ })
95
+
96
+ test('hash_compare_returns_promise_on_null_callback', () => {
97
+ expect(typeof fastbcrypt.compare('password', 'something', null).then).toStrictEqual('function')
98
+ })
99
+
100
+ test('hash_compare', () => {
101
+ expect.assertions(3);
102
+ return fastbcrypt.genSalt(10).then(function (salt) {
103
+ expect(salt).toHaveLength(29);
104
+ return fastbcrypt.hash("test", salt);
105
+ }).then(hash => Promise.all(
106
+ [
107
+ expect(fastbcrypt.compare("test", hash)).resolves.toEqual(true),
108
+ expect(fastbcrypt.compare("blah", hash)).resolves.toEqual(false)
109
+ ]));
110
+ })
111
+
112
+ test('hash_compare_empty_strings', () => {
113
+ expect.assertions(2);
114
+ const hash = fastbcrypt.hashSync("test", fastbcrypt.genSaltSync(10));
115
+ return Promise.all([
116
+ expect(fastbcrypt.compare("", hash)).resolves.toEqual(false),
117
+ expect(fastbcrypt.compare("", "")).resolves.toEqual(false)
118
+ ]);
119
+ })
120
+
121
+ test('hash_compare_invalid_strings', () => {
122
+ const fullString = 'envy1362987212538';
123
+ const hash = '$2a$10$XOPbrlUPQdwdJUpSrIF6X.LbE14qsMmKGhM1A8W9iqaG3vv1BD7WC';
124
+ const wut = ':';
125
+ return Promise.all([
126
+ expect(fastbcrypt.compare(fullString, hash)).resolves.toEqual(true),
127
+ expect(fastbcrypt.compare(fullString, wut)).resolves.toEqual(false),
128
+ ]);
129
+ })
130
+
131
+ test('hash_compare_no_params', () => {
132
+ expect.assertions(1);
133
+ return expect(fastbcrypt.compare()).rejects.toThrow('data and hash arguments required')
134
+ })
135
+
136
+ test('hash_compare_one_param', () => {
137
+ expect.assertions(1);
138
+ return expect(fastbcrypt.compare('password')).rejects.toThrow('data and hash arguments required')
139
+ })
140
+
141
+ test('change_promise_impl_reject', () => {
142
+
143
+ promises.use({
144
+ reject: function () {
145
+ return 'mock';
146
+ }
147
+ });
148
+
149
+ expect(promises.reject()).toEqual('mock');
150
+
151
+ // need to reset the promise implementation because of require cache
152
+ promises.use(global.Promise);
153
+ })
154
+
155
+ test('change_promise_impl_promise', () => {
156
+
157
+ promises.use({
158
+ reject: function (err) {
159
+ expect(err.message).toEqual('fn must be a function');
160
+ return 'mock';
161
+ }
162
+ });
163
+
164
+ expect(promises.promise('', '', '')).toEqual('mock');
165
+
166
+ // need to reset the promise implementation because of require cache
167
+ promises.use(global.Promise);
168
+ })
@@ -0,0 +1,55 @@
1
+ const fastbcrypt = require('..');
2
+
3
+ const EXPECTED = 2500; //number of times to iterate these tests.)
4
+ const { TEST_TIMEOUT_SECONDS } = process.env;
5
+ let timeout = 5e3; // default test timeout
6
+
7
+ // it is necessary to increase the test timeout when emulating cross-architecture
8
+ // environments (i.e. arm64 from x86-64 host) which have significantly reduced performance:
9
+ if ( TEST_TIMEOUT_SECONDS )
10
+ timeout = Number.parseInt(TEST_TIMEOUT_SECONDS, 10) * 1e3;
11
+
12
+ jest.setTimeout(timeout);
13
+
14
+ test('salt_length', () => {
15
+ expect.assertions(EXPECTED);
16
+
17
+ return Promise.all(Array.from({length: EXPECTED},
18
+ () => fastbcrypt.genSalt(10)
19
+ .then(salt => expect(salt).toHaveLength(29))));
20
+ })
21
+
22
+ test('test_hash_length', () => {
23
+ expect.assertions(EXPECTED);
24
+ const SALT = '$2a$04$TnjywYklQbbZjdjBgBoA4e';
25
+ return Promise.all(Array.from({length: EXPECTED},
26
+ () => fastbcrypt.hash('test', SALT)
27
+ .then(hash => expect(hash).toHaveLength(60))));
28
+ })
29
+
30
+ test('test_compare', () => {
31
+ expect.assertions(EXPECTED);
32
+ const HASH = '$2a$04$TnjywYklQbbZjdjBgBoA4e9G7RJt9blgMgsCvUvus4Iv4TENB5nHy';
33
+ return Promise.all(Array.from({length: EXPECTED},
34
+ () => fastbcrypt.compare('test', HASH)
35
+ .then(match => expect(match).toEqual(true))));
36
+ })
37
+
38
+ test('test_hash_and_compare', () => {
39
+ expect.assertions(EXPECTED * 3);
40
+ const salt = fastbcrypt.genSaltSync(4)
41
+
42
+ return Promise.all(Array.from({length: EXPECTED},
43
+ () => {
44
+ const password = 'secret' + Math.random();
45
+ return fastbcrypt.hash(password, salt)
46
+ .then(hash => {
47
+ expect(hash).toHaveLength(60);
48
+ const goodCompare = fastbcrypt.compare(password, hash).then(res => expect(res).toEqual(true));
49
+ const badCompare = fastbcrypt.compare('bad' + password, hash).then(res => expect(res).toEqual(false));
50
+
51
+ return Promise.all([goodCompare, badCompare]);
52
+ });
53
+ }));
54
+ }, timeout * 3);
55
+