* Internaly crypts a string. * @param {Array. } b Bytes to crypt * @param {Array. } salt Salt bytes to use * @param {number} rounds Number of rounds * @param {function(Error, Array. =)=} callback Callback receiving the error, if any, and the resulting bytes. If * omitted,
(b, salt, rounds, callback, progressCallback)
| 940 | * @inner |
| 941 | */ |
| 942 | function _crypt(b, salt, rounds, callback, progressCallback) { |
| 943 | var cdata = C_ORIG.slice(), |
| 944 | clen = cdata.length, |
| 945 | err; |
| 946 | |
| 947 | // Validate |
| 948 | if (rounds < 4 || rounds > 31) { |
| 949 | err = Error("Illegal number of rounds (4-31): " + rounds); |
| 950 | if (callback) { |
| 951 | nextTick(callback.bind(this, err)); |
| 952 | return; |
| 953 | } else throw err; |
| 954 | } |
| 955 | if (salt.length !== BCRYPT_SALT_LEN) { |
| 956 | err = Error( |
| 957 | "Illegal salt length: " + salt.length + " != " + BCRYPT_SALT_LEN, |
| 958 | ); |
| 959 | if (callback) { |
| 960 | nextTick(callback.bind(this, err)); |
| 961 | return; |
| 962 | } else throw err; |
| 963 | } |
| 964 | rounds = (1 << rounds) >>> 0; |
| 965 | |
| 966 | var P, |
| 967 | S, |
| 968 | i = 0, |
| 969 | j; |
| 970 | |
| 971 | //Use typed arrays when available - huge speedup! |
| 972 | if (typeof Int32Array === "function") { |
| 973 | P = new Int32Array(P_ORIG); |
| 974 | S = new Int32Array(S_ORIG); |
| 975 | } else { |
| 976 | P = P_ORIG.slice(); |
| 977 | S = S_ORIG.slice(); |
| 978 | } |
| 979 | |
| 980 | _ekskey(salt, b, P, S); |
| 981 | |
| 982 | /** |
| 983 | * Calcualtes the next round. |
| 984 | * @returns {Array.<number>|undefined} Resulting array if callback has been omitted, otherwise `undefined` |
| 985 | * @inner |
| 986 | */ |
| 987 | function next() { |
| 988 | if (progressCallback) progressCallback(i / rounds); |
| 989 | if (i < rounds) { |
| 990 | var start = Date.now(); |
| 991 | for (; i < rounds; ) { |
| 992 | i = i + 1; |
| 993 | _key(b, P, S); |
| 994 | _key(salt, P, S); |
| 995 | if (Date.now() - start > MAX_EXECUTION_TIME) break; |
| 996 | } |
| 997 | } else { |
| 998 | for (i = 0; i < 64; i++) |
| 999 | for (j = 0; j < clen >> 1; j++) _encipher(cdata, j << 1, P, S); |