This repository was archived by the owner on Feb 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCryptoJsPasswordEncoder.js
More file actions
66 lines (60 loc) · 2.1 KB
/
CryptoJsPasswordEncoder.js
File metadata and controls
66 lines (60 loc) · 2.1 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
(function ( root, factory ) {
if ( typeof exports === 'object' ) {
// CommonJS
factory( exports );
} else if ( typeof define === 'function' && define.amd ) {
// AMD. Register as an anonymous module.
define( ['exports'], factory);
} else {
// Browser globals
factory(root);
}
}(this, function ( exports, b ) {
function CryptoJsPasswordEncoder(algorithm, encodeHashAsBase64, iterations){
if (!algorithm) {
algorithm = 'sha512';
}
if (!encodeHashAsBase64) {
encodeHashAsBase64 = true;
}
if (!iterations) {
iterations = 5000;
}
this.algorithm = algorithm;
this.encodeHashAsBase64 = encodeHashAsBase64;
this.iterations = iterations;
}
CryptoJsPasswordEncoder.VERSION = '0.1.1';
CryptoJsPasswordEncoder.prototype.isPasswordTooLong = function(raw) {
return raw.length > 4096;
};
CryptoJsPasswordEncoder.prototype.mergePasswordAndSalt = function(password, salt) {
if (!salt) {
return password;
}
if (salt.indexOf('{') > -1 || salt.indexOf('}') > -1) {
throw 'Cannot use { or } in salt.';
}
return password + '{' + salt + '}';
};
CryptoJsPasswordEncoder.prototype.encodePassword = function(raw, salt) {
if (this.isPasswordTooLong(raw)) {
throw new Error("Invalid password");
}
var algorithm = this.algorithm.toUpperCase();
if (!CryptoJS[algorithm]) {
throw new Error("'The algorithm "+this.algorithm+" is not supported.'");
}
var salted = this.mergePasswordAndSalt(raw, salt);
var digest = CryptoJS[algorithm](salted);
for ( var i = 0; i < this.iterations - 1; i++) {
digest = CryptoJS[algorithm](digest.concat(CryptoJS.enc.Utf8.parse(salted)));
}
if (this.encodeHashAsBase64) {
return digest.toString(CryptoJS.enc.Base64);
} else {
return digest;
}
};
exports.CryptoJsPasswordEncoder = CryptoJsPasswordEncoder;
}));