sha512-crypt-ts
Advanced tools
Comparing version 0.1.9 to 0.1.10
/** | ||
* SHA-512 crypt compatible module | ||
* SHA-512, supporting hex, base64, crypt and HMAC hashes | ||
* @preferred | ||
@@ -7,5 +7,6 @@ */ | ||
/** | ||
* Compute SHA-512 hash compatible with crypt implementation (mkpasswd --method=sha-512) | ||
* Compute SHA-512 hash compatible with crypt implementation (`mkpasswd --method=sha-512`) | ||
* @param input - Input string to be hashed | ||
* @param salt - Salt to be used with algorithm. Can contain magic prefix. Eg. param `$6$rounds=1000$saltvalue` Will use version 6 of sha-512 with rounds decreased from default 5000 to 1000 and salt = `saltvalue` | ||
* @param salt - Salt to be used with algorithm. Salt length must be between 8 and 16 characters. Can contain magic prefix. Eg. param `$6$rounds=1000$saltvalue` Will use rounds decreased from default 5000 to 1000 and salt = `saltvalue` | ||
* @throws Error if input values validation fails | ||
*/ | ||
@@ -12,0 +13,0 @@ const crypt: (input: string, salt: string) => string; |
@@ -12,5 +12,11 @@ "use strict"; | ||
function Delegate() { | ||
this.hexcase = 0; /* hex output format. 0 - lowercase; 1 - uppercase */ | ||
this.b64pad = ""; /* base-64 pad character. "=" for strict RFC compliance */ | ||
/* | ||
/** | ||
* hex output format. 0 - lowercase; 1 - uppercase | ||
*/ | ||
this.hexcase = 0; | ||
/** | ||
* base-64 pad character. "=" for strict RFC compliance | ||
*/ | ||
this.b64pad = ""; | ||
/** | ||
* Calculate the SHA-512 of an array of big-endian dwords, and a bit length | ||
@@ -20,3 +26,3 @@ */ | ||
} | ||
/* | ||
/** | ||
* Calculate the SHA-512 of a raw string | ||
@@ -27,3 +33,3 @@ */ | ||
}; | ||
/* | ||
/** | ||
* Calculate the HMAC-SHA-512 of a key and some data (raw strings) | ||
@@ -43,3 +49,3 @@ */ | ||
}; | ||
/* | ||
/** | ||
* Convert a raw string to a hex string | ||
@@ -58,3 +64,3 @@ */ | ||
}; | ||
/* | ||
/** | ||
* Convert a raw string to a base-64 string | ||
@@ -79,3 +85,3 @@ */ | ||
}; | ||
/* | ||
/** | ||
* Convert a raw string to an arbitrary string encoding | ||
@@ -119,3 +125,3 @@ */ | ||
}; | ||
/* | ||
/** | ||
* Encode a string as utf-8. | ||
@@ -148,3 +154,3 @@ * For efficiency, this assumes the input is valid utf-16. | ||
}; | ||
/* | ||
/** | ||
* Convert a raw string to an array of big-endian words | ||
@@ -161,3 +167,3 @@ * Characters >255 have their high-byte silently ignored. | ||
}; | ||
/* | ||
/** | ||
* Convert an array of big-endian words to a string | ||
@@ -236,10 +242,10 @@ */ | ||
{ | ||
Delegate.Int64copy(a, H[0]); | ||
Delegate.Int64copy(b, H[1]); | ||
Delegate.Int64copy(c, H[2]); | ||
Delegate.Int64copy(d, H[3]); | ||
Delegate.Int64copy(e, H[4]); | ||
Delegate.Int64copy(f, H[5]); | ||
Delegate.Int64copy(g, H[6]); | ||
Delegate.Int64copy(h, H[7]); | ||
Delegate.int64copy(a, H[0]); | ||
Delegate.int64copy(b, H[1]); | ||
Delegate.int64copy(c, H[2]); | ||
Delegate.int64copy(d, H[3]); | ||
Delegate.int64copy(e, H[4]); | ||
Delegate.int64copy(f, H[5]); | ||
Delegate.int64copy(g, H[6]); | ||
Delegate.int64copy(h, H[7]); | ||
for (j = 0; j < 16; j++) { | ||
@@ -251,14 +257,14 @@ W[j].h = x[i + 2 * j]; | ||
//sigma1 | ||
Delegate.Int64rrot(r1, W[j - 2], 19); | ||
Delegate.Int64revrrot(r2, W[j - 2], 29); | ||
Delegate.Int64shr(r3, W[j - 2], 6); | ||
Delegate.int64rrot(r1, W[j - 2], 19); | ||
Delegate.int64revrrot(r2, W[j - 2], 29); | ||
Delegate.int64shr(r3, W[j - 2], 6); | ||
s1.l = r1.l ^ r2.l ^ r3.l; | ||
s1.h = r1.h ^ r2.h ^ r3.h; | ||
//sigma0 | ||
Delegate.Int64rrot(r1, W[j - 15], 1); | ||
Delegate.Int64rrot(r2, W[j - 15], 8); | ||
Delegate.Int64shr(r3, W[j - 15], 7); | ||
Delegate.int64rrot(r1, W[j - 15], 1); | ||
Delegate.int64rrot(r2, W[j - 15], 8); | ||
Delegate.int64shr(r3, W[j - 15], 7); | ||
s0.l = r1.l ^ r2.l ^ r3.l; | ||
s0.h = r1.h ^ r2.h ^ r3.h; | ||
Delegate.Int64add4(W[j], s1, W[j - 7], s0, W[j - 16]); | ||
Delegate.int64add4(W[j], s1, W[j - 7], s0, W[j - 16]); | ||
} | ||
@@ -270,11 +276,11 @@ for (j = 0; j < 80; j++) { | ||
//Sigma1 | ||
Delegate.Int64rrot(r1, e, 14); | ||
Delegate.Int64rrot(r2, e, 18); | ||
Delegate.Int64revrrot(r3, e, 9); | ||
Delegate.int64rrot(r1, e, 14); | ||
Delegate.int64rrot(r2, e, 18); | ||
Delegate.int64revrrot(r3, e, 9); | ||
s1.l = r1.l ^ r2.l ^ r3.l; | ||
s1.h = r1.h ^ r2.h ^ r3.h; | ||
//Sigma0 | ||
Delegate.Int64rrot(r1, a, 28); | ||
Delegate.Int64revrrot(r2, a, 2); | ||
Delegate.Int64revrrot(r3, a, 7); | ||
Delegate.int64rrot(r1, a, 28); | ||
Delegate.int64revrrot(r2, a, 2); | ||
Delegate.int64revrrot(r3, a, 7); | ||
s0.l = r1.l ^ r2.l ^ r3.l; | ||
@@ -285,21 +291,21 @@ s0.h = r1.h ^ r2.h ^ r3.h; | ||
Maj.h = (a.h & b.h) ^ (a.h & c.h) ^ (b.h & c.h); | ||
Delegate.Int64add5(T1, h, s1, Ch, this.sha512_k[j], W[j]); | ||
Delegate.Int64add(T2, s0, Maj); | ||
Delegate.Int64copy(h, g); | ||
Delegate.Int64copy(g, f); | ||
Delegate.Int64copy(f, e); | ||
Delegate.Int64add(e, d, T1); | ||
Delegate.Int64copy(d, c); | ||
Delegate.Int64copy(c, b); | ||
Delegate.Int64copy(b, a); | ||
Delegate.Int64add(a, T1, T2); | ||
Delegate.int64add5(T1, h, s1, Ch, this.sha512_k[j], W[j]); | ||
Delegate.int64add(T2, s0, Maj); | ||
Delegate.int64copy(h, g); | ||
Delegate.int64copy(g, f); | ||
Delegate.int64copy(f, e); | ||
Delegate.int64add(e, d, T1); | ||
Delegate.int64copy(d, c); | ||
Delegate.int64copy(c, b); | ||
Delegate.int64copy(b, a); | ||
Delegate.int64add(a, T1, T2); | ||
} | ||
Delegate.Int64add(H[0], H[0], a); | ||
Delegate.Int64add(H[1], H[1], b); | ||
Delegate.Int64add(H[2], H[2], c); | ||
Delegate.Int64add(H[3], H[3], d); | ||
Delegate.Int64add(H[4], H[4], e); | ||
Delegate.Int64add(H[5], H[5], f); | ||
Delegate.Int64add(H[6], H[6], g); | ||
Delegate.Int64add(H[7], H[7], h); | ||
Delegate.int64add(H[0], H[0], a); | ||
Delegate.int64add(H[1], H[1], b); | ||
Delegate.int64add(H[2], H[2], c); | ||
Delegate.int64add(H[3], H[3], d); | ||
Delegate.int64add(H[4], H[4], e); | ||
Delegate.int64add(H[5], H[5], f); | ||
Delegate.int64add(H[6], H[6], g); | ||
Delegate.int64add(H[7], H[7], h); | ||
} | ||
@@ -314,29 +320,39 @@ //represent the hash as an array of 32-bit dwords | ||
}; | ||
//Copies src into dst, assuming both are 64-bit numbers | ||
Delegate.Int64copy = function (dst, src) { | ||
/** | ||
* Copies src into dst, assuming both are 64-bit numbers | ||
*/ | ||
Delegate.int64copy = function (dst, src) { | ||
dst.h = src.h; | ||
dst.l = src.l; | ||
}; | ||
//Right-rotates a 64-bit number by shift | ||
//Won't handle cases of shift>=32 | ||
//The private revrrot() is for that | ||
Delegate.Int64rrot = function (dst, x, shift) { | ||
/** | ||
* Right-rotates a 64-bit number by shift | ||
* Won't handle cases of shift>=32 | ||
* The private revrrot() is for that | ||
* */ | ||
Delegate.int64rrot = function (dst, x, shift) { | ||
dst.l = (x.l >>> shift) | (x.h << (32 - shift)); | ||
dst.h = (x.h >>> shift) | (x.l << (32 - shift)); | ||
}; | ||
//Reverses the dwords of the source and then rotates right by shift. | ||
//This is equivalent to rotation by 32+shift | ||
Delegate.Int64revrrot = function (dst, x, shift) { | ||
/** | ||
* Reverses the dwords of the source and then rotates right by shift. | ||
* This is equivalent to rotation by 32+shift | ||
*/ | ||
Delegate.int64revrrot = function (dst, x, shift) { | ||
dst.l = (x.h >>> shift) | (x.l << (32 - shift)); | ||
dst.h = (x.l >>> shift) | (x.h << (32 - shift)); | ||
}; | ||
//Bitwise-shifts right a 64-bit number by shift | ||
//Won't handle shift>=32, but it's never needed in SHA512 | ||
Delegate.Int64shr = function (dst, x, shift) { | ||
/** | ||
* Bitwise-shifts right a 64-bit number by shift | ||
* Won't handle shift>=32, but it's never needed in SHA512 | ||
*/ | ||
Delegate.int64shr = function (dst, x, shift) { | ||
dst.l = (x.l >>> shift) | (x.h << (32 - shift)); | ||
dst.h = (x.h >>> shift); | ||
}; | ||
//Adds two 64-bit numbers | ||
//Like the original implementation, does not rely on 32-bit operations | ||
Delegate.Int64add = function (dst, x, y) { | ||
/** | ||
* Adds two 64-bit numbers | ||
* Like the original implementation, does not rely on 32-bit operations | ||
*/ | ||
Delegate.int64add = function (dst, x, y) { | ||
var w0 = (x.l & 0xffff) + (y.l & 0xffff); | ||
@@ -349,4 +365,6 @@ var w1 = (x.l >>> 16) + (y.l >>> 16) + (w0 >>> 16); | ||
}; | ||
//Same, except with 4 addends. Works faster than adding them one by one. | ||
Delegate.Int64add4 = function (dst, a, b, c, d) { | ||
/** | ||
* Adds two 64-bit numbers with 4 addends. Works faster than adding them one by one. | ||
*/ | ||
Delegate.int64add4 = function (dst, a, b, c, d) { | ||
var w0 = (a.l & 0xffff) + (b.l & 0xffff) + (c.l & 0xffff) + (d.l & 0xffff); | ||
@@ -359,4 +377,6 @@ var w1 = (a.l >>> 16) + (b.l >>> 16) + (c.l >>> 16) + (d.l >>> 16) + (w0 >>> 16); | ||
}; | ||
//Same, except with 5 addends | ||
Delegate.Int64add5 = function (dst, a, b, c, d, e) { | ||
/** | ||
* Adds two 64-bit numbers with 5 addends. Works faster than adding them one by one. | ||
*/ | ||
Delegate.int64add5 = function (dst, a, b, c, d, e) { | ||
var w0 = (a.l & 0xffff) + (b.l & 0xffff) + (c.l & 0xffff) + (d.l & 0xffff) + (e.l & 0xffff); | ||
@@ -438,4 +458,3 @@ var w1 = (a.l >>> 16) + (b.l >>> 16) + (c.l >>> 16) + (d.l >>> 16) + (e.l >>> 16) + (w0 >>> 16); | ||
if (magic_array[1] !== "6") { | ||
var s = "Got '" + salt + "' but only SHA512 ($6$) algorithm supported"; | ||
throw new Error(s); | ||
throw new Error("Got '" + salt + "' but only SHA512 ($6$) algorithm supported"); | ||
} | ||
@@ -455,3 +474,5 @@ rounds = parseInt(magic_array[2].split("=")[1]); | ||
// salt is max 16 chars long | ||
salt = salt.substr(0, 16); | ||
if (salt.length < 8 || salt.length > 16) { | ||
throw new Error("Wrong salt length: " + salt.length + " bytes when 8 <= n <= 16 expected. Got salt " + salt + "."); | ||
} | ||
var input = this._rstr_sha512crypt(password, salt, rounds || 5000); | ||
@@ -514,3 +535,3 @@ var output = ""; | ||
/** | ||
* SHA-512 crypt compatible module | ||
* SHA-512, supporting hex, base64, crypt and HMAC hashes | ||
* @preferred | ||
@@ -527,5 +548,6 @@ */ | ||
/** | ||
* Compute SHA-512 hash compatible with crypt implementation (mkpasswd --method=sha-512) | ||
* Compute SHA-512 hash compatible with crypt implementation (`mkpasswd --method=sha-512`) | ||
* @param input - Input string to be hashed | ||
* @param salt - Salt to be used with algorithm. Can contain magic prefix. Eg. param `$6$rounds=1000$saltvalue` Will use version 6 of sha-512 with rounds decreased from default 5000 to 1000 and salt = `saltvalue` | ||
* @param salt - Salt to be used with algorithm. Salt length must be between 8 and 16 characters. Can contain magic prefix. Eg. param `$6$rounds=1000$saltvalue` Will use rounds decreased from default 5000 to 1000 and salt = `saltvalue` | ||
* @throws Error if input values validation fails | ||
*/ | ||
@@ -532,0 +554,0 @@ sha512.crypt = function (input, salt) { return delegate.sha512crypt(input, salt); }; |
{ | ||
"name": "sha512-crypt-ts", | ||
"version": "0.1.9", | ||
"version": "0.1.10", | ||
"description": "Typescript wrapper for SHA-512, including $6$ crypt format.", | ||
@@ -5,0 +5,0 @@ "scripts": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
31553
652