Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 | 1x 38x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 26x 12x 26x 26x 18x 18x 18x 8x 8x 8x 26x 26x 26x 26x 106x 106x 26x 26x 26x 26x 26x 26x 1664x 26x 26x 26x 26x 26x 26x 1664x 26x 26x 26x 26x 26x 26x 1664x 26x 26x 26x 26x 26x 26x 26x 54x 54x 54x 54x 54x 54x 110064x 110064x 110064x 54x 54x 54x 3456x 54x 54x 54x 54x 54x 108x 108x 108x 54x 108x 108x 6912x 6912x 6912x 108x 54x 54x 54x 54x 26x 26x 26x 26x 2x 2x 2x 2x 2x 2x 2x 2x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 188x 7x 1x 1x 1x 1x 1x 1x 1x | const crypto = require("crypto");
//**************************************************************************************
function getRandomValues(length)
{
return (new Uint8Array(crypto.randomBytes(length)));
}
//**************************************************************************************
function pbkdf1(password, salt, iterationCount, keyLength, hashAlgorithm)
{
let key = Buffer.concat([password, salt]);
for(let i = 0; i < iterationCount; i++)
key = crypto.createHash(hashAlgorithm).update(key).digest();
return key.slice(0, keyLength);
}
//**************************************************************************************
/**
* encryptUsingPBKDF2Password
* @param {string} algorithm
* @param {number} keyLength
* @param {ArrayBuffer} password
* @param {ArrayBuffer} salt
* @param {number} iterationCount
* @param {string} hashAlgorithm
* @param {ArrayBuffer} iv
* @param {ArrayBuffer} messageToEncrypt
*/
function encryptUsingPBKDF2Password(algorithm, keyLength, password, salt, iterationCount, hashAlgorithm, iv, messageToEncrypt)
{
//region Initial variables
let cipher;
//endregion
//region Make hash algorithm name to be Node-friendly
hashAlgorithm = hashAlgorithm.replace("-", "");
//endregion
//region Derive key using PBKDF2 algorithm
const key = crypto.pbkdf2Sync(Buffer.from(password), Buffer.from(salt), iterationCount, keyLength, hashAlgorithm);
//endregion
//region Initialize cipher object
Eif(iv.byteLength)
cipher = crypto.createCipheriv(algorithm, key, Buffer.from(iv));
else
cipher = crypto.createCipher(algorithm, key);
//endregion
return (new Uint8Array(Buffer.concat([cipher.update(Buffer.from(messageToEncrypt)), cipher.final()]))).buffer;
}
//**************************************************************************************
/**
* decryptUsingPBKDF2Password
* @param {string} algorithm
* @param {number} keyLength
* @param {ArrayBuffer} password
* @param {ArrayBuffer} salt
* @param {number} iterationCount
* @param {string} hashAlgorithm
* @param {ArrayBuffer} iv
* @param {ArrayBuffer} messageToDecrypt
*/
function decryptUsingPBKDF2Password(algorithm, keyLength, password, salt, iterationCount, hashAlgorithm, iv, messageToDecrypt)
{
//region Initial variables
let cipher;
//endregion
//region Make hash algorithm name to be Node-friendly
hashAlgorithm = hashAlgorithm.replace("-", "");
//endregion
//region Derive key using PBKDF2 algorithm
const key = crypto.pbkdf2Sync(Buffer.from(password), Buffer.from(salt), iterationCount, keyLength, hashAlgorithm);
//endregion
//region Initialize cipher object
Eif(iv.byteLength)
cipher = crypto.createDecipheriv(algorithm, key, Buffer.from(iv));
else
cipher = crypto.createDecipher(algorithm, key);
//endregion
return (new Uint8Array(Buffer.concat([cipher.update(Buffer.from(messageToDecrypt)), cipher.final()]))).buffer;
}
//**************************************************************************************
function makePKCS12B2Key(hashAlgorithm, keyLength, password, salt, iterationCount, id)
{
//region Check input "method" value
if((typeof id === "undefined"))
id = 3; // For MACing
//endregion
//region Initial variables
let u;
let v;
const result = [];
//endregion
//region Get "u" and "v" values
switch(hashAlgorithm.toUpperCase())
{
case "SHA1":
u = 20; // 160
v = 64; // 512
break;
case "SHA256":
u = 32; // 256
v = 64; // 512
break;
case "SHA384":
u = 48; // 384
v = 128; // 1024
break;
case "SHA512":
u = 64; // 512
v = 128; // 1024
break;
default:
throw new Error("Unsupported hashing algorithm");
}
//endregion
//region Main algorithm making key
//region Transform password to UTF-8 like string
const passwordViewInitial = new Uint8Array(password);
const passwordTransformed = new ArrayBuffer((password.byteLength * 2) + 2);
const passwordTransformedView = new Uint8Array(passwordTransformed);
for(let i = 0; i < passwordViewInitial.length; i++)
{
passwordTransformedView[i * 2] = 0x00;
passwordTransformedView[i * 2 + 1] = passwordViewInitial[i];
}
passwordTransformedView[passwordTransformedView.length - 2] = 0x00;
passwordTransformedView[passwordTransformedView.length - 1] = 0x00;
password = passwordTransformed.slice(0);
//endregion
//region Construct a string D (the "diversifier") by concatenating v/8 copies of ID
const D = new ArrayBuffer(v);
const dView = new Uint8Array(D);
for(let i = 0; i < D.byteLength; i++)
dView[i] = id; // The ID value equal to "3" for MACing (see B.3 of standard)
//endregion
//region Concatenate copies of the salt together to create a string S of length v * ceil(s / v) bytes (the final copy of the salt may be trunacted to create S)
const saltLength = salt.byteLength;
const sLen = v * Math.ceil(saltLength / v);
const S = new ArrayBuffer(sLen);
const sView = new Uint8Array(S);
const saltView = new Uint8Array(salt);
for(let i = 0; i < sLen; i++)
sView[i] = saltView[i % saltLength];
//endregion
//region Concatenate copies of the password together to create a string P of length v * ceil(p / v) bytes (the final copy of the password may be truncated to create P)
const passwordLength = password.byteLength;
const pLen = v * Math.ceil(passwordLength / v);
const P = new ArrayBuffer(pLen);
const pView = new Uint8Array(P);
const passwordView = new Uint8Array(password);
for(let i = 0; i < pLen; i++)
pView[i] = passwordView[i % passwordLength];
//endregion
//region Set I=S||P to be the concatenation of S and P
const sPlusPLength = S.byteLength + P.byteLength;
let I = new ArrayBuffer(sPlusPLength);
let iView = new Uint8Array(I);
iView.set(sView);
iView.set(pView, sView.length);
//endregion
//region Set c=ceil(n / u)
const c = Math.ceil((keyLength >> 3) / u);
//endregion
//region For i=1, 2, ..., c, do the following:
for(let i = 0; i <= c; i++)
{
//region Create contecanetion of D and I
const dAndI = new ArrayBuffer(D.byteLength + I.byteLength);
const dAndIView = new Uint8Array(dAndI);
dAndIView.set(dView);
dAndIView.set(iView, dView.length);
//endregion
//region Make "iterationCount" rounds of hashing
let roundBuffer = Buffer.from(dAndI);
for(let j = 0; j < iterationCount; j++)
{
const hash = crypto.createHash(hashAlgorithm);
hash.update(roundBuffer);
roundBuffer = hash.digest();
}
//endregion
//region Concatenate copies of Ai to create a string B of length v bits (the final copy of Ai may be truncated to create B)
const B = new ArrayBuffer(v);
const bView = new Uint8Array(B);
for(let j = 0; j < B.byteLength; j++)
bView[j] = roundBuffer[j % roundBuffer.length];
//endregion
//region Make new I value
const k = Math.ceil(saltLength / v) + Math.ceil(passwordLength / v);
const iRound = [];
let sliceStart = 0;
let sliceLength = v;
for(let j = 0; j < k; j++)
{
const chunk = Array.from(new Uint8Array(I.slice(sliceStart, sliceStart + sliceLength)));
sliceStart += v;
if((sliceStart + v) > I.byteLength)
sliceLength = I.byteLength - sliceStart;
let x = 0x1ff;
for(let l = (B.byteLength - 1); l >= 0; l--)
{
x = x >> 8;
x += bView[l] + chunk[l];
chunk[l] = (x & 0xff);
}
iRound.push(...chunk);
}
I = new ArrayBuffer(iRound.length);
iView = new Uint8Array(I);
iView.set(iRound);
//endregion
result.push(...roundBuffer);
}
//endregion
//region Initialize final key
const resultBuffer = new ArrayBuffer(keyLength >> 3);
const resultView = new Uint8Array(resultBuffer);
resultView.set((new Uint8Array(result)).slice(0, keyLength >> 3));
//endregion
//endregion
return Buffer.from(resultBuffer);
}
//**************************************************************************************
/**
* encryptUsingPBKDF1Password
* @param {string} algorithm
* @param {number} keyLength In bytes
* @param {number} ivLength In bytes
* @param {ArrayBuffer} password
* @param {ArrayBuffer} salt
* @param {number} iterationCount
* @param {ArrayBuffer} messageToEncrypt
* @param {string} [method=pkcs12]
*/
function encryptUsingPBKDF1Password(algorithm, keyLength, ivLength, password, salt, iterationCount, messageToEncrypt, method)
{
//region Check input "method" value
Eif((typeof method === "undefined"))
method = "pkcs12";
//endregion
//region Initial variables
let key;
let iv;
//endregion
//region Generate key and IV using selected generation method
switch(method.toLowerCase())
{
//region PKCS#12 key derivation function
case "pkcs12":
key = makePKCS12B2Key("SHA1", keyLength << 3, password, salt, iterationCount, 1);
iv = makePKCS12B2Key("SHA1", ivLength << 3, password, salt, iterationCount, 2);
break;
//endregion
//region PKCS#5 key derivation function
default:
{
const pbkdf1Result = pbkdf1(Buffer.from(password), Buffer.from(salt), iterationCount, 16, "SHA1");
key = pbkdf1Result.slice(0, 8);
iv = pbkdf1Result.slice(8);
}
//endregion
}
//endregion
//region Initialize cipher object
const cipher = crypto.createCipheriv(algorithm, key, iv);
//endregion
return (new Uint8Array(Buffer.concat([cipher.update(Buffer.from(messageToEncrypt)), cipher.final()]))).buffer;
}
//**************************************************************************************
/**
* decryptUsingPBKDF1Password
* @param {string} algorithm
* @param {number} keyLength In bytes
* @param {number} ivLength In bytes
* @param {ArrayBuffer} password
* @param {ArrayBuffer} salt
* @param {number} iterationCount
* @param {ArrayBuffer} messageToDecrypt
* @param {string} [method=pkcs12]
*/
function decryptUsingPBKDF1Password(algorithm, keyLength, ivLength, password, salt, iterationCount, messageToDecrypt, method)
{
//region Check input "method" value
Eif((typeof method === "undefined"))
method = "pkcs12";
//endregion
//region Initial variables
let key;
let iv;
//endregion
//region Generate key and IV using selected generation method
switch(method.toLowerCase())
{
//region PKCS#12 key derivation function
case "pkcs12":
key = makePKCS12B2Key("SHA1", keyLength << 3, password, salt, iterationCount, 1);
iv = makePKCS12B2Key("SHA1", ivLength << 3, password, salt, iterationCount, 2);
break;
//endregion
//region PKCS#5 key derivation function
default:
{
const pbkdf1Result = pbkdf1(Buffer.from(password), Buffer.from(salt), iterationCount, 16, "SHA1");
key = pbkdf1Result.slice(0, 8);
iv = pbkdf1Result.slice(8);
}
//endregion
}
//endregion
//region Initialize cipher object
const cipher = crypto.createDecipheriv(algorithm, key, iv);
//endregion
return (new Uint8Array(Buffer.concat([cipher.update(Buffer.from(messageToDecrypt)), cipher.final()]))).buffer;
}
//**************************************************************************************
/**
* Making MAC data using algorithm described in B.2 of PKCS#12 standard.
*/
function stampDataWithPassword(hashAlgorithm, keyLength, password, salt, iterationCount, stampingData, method)
{
//region Initial variables
let key;
//endregion
//region Check input "method" value
Eif((typeof method === "undefined"))
method = "pkcs12";
//endregion
//region Make hash algorithm name to be Node-friendly
hashAlgorithm = hashAlgorithm.replace("-", "");
//endregion
//region Derive key using PKCS#12 algorithm from B.2 item of standard
switch(method.toLowerCase())
{
case "pbkdf2":
key = crypto.pbkdf2Sync(Buffer.from(password), Buffer.from(salt), iterationCount, keyLength, hashAlgorithm);
break;
case "pkcs12":
default:
key = makePKCS12B2Key(hashAlgorithm, keyLength, password, salt, iterationCount);
}
//endregion
//region Making HMAC value
const hmac = crypto.createHmac(hashAlgorithm, key);
hmac.update(Buffer.from(stampingData));
//endregion
return (new Uint8Array(hmac.digest())).buffer;
}
//**************************************************************************************
function verifyDataStampedWithPassword(hashAlgorithm, keyLength, password, salt, iterationCount, stampedData, signatureToVerify, method)
{
//region Initial variables
let key;
//endregion
//region Check input "method" value
Eif((typeof method === "undefined"))
method = "pkcs12";
//endregion
//region Make hash algorithm name to be Node-friendly
hashAlgorithm = hashAlgorithm.replace("-", "");
//endregion
//region Derive key using PKCS#12 algorithm from B.2 item of standard
switch(method.toLowerCase())
{
case "pbkdf2":
key = crypto.pbkdf2Sync(Buffer.from(password), Buffer.from(salt), iterationCount, keyLength, hashAlgorithm);
break;
case "pkcs12":
default:
key = makePKCS12B2Key(hashAlgorithm, keyLength, password, salt, iterationCount);
}
//endregion
//region Making HMAC value
const hmac = crypto.createHmac(hashAlgorithm, key);
hmac.update(Buffer.from(stampedData));
const hmacValue = new Uint8Array(hmac.digest());
//endregion
//region Compare HMAC digest with signature to verify
const dataView = new Uint8Array(signatureToVerify);
Iif(hmacValue.length !== dataView.length)
return false;
let result = true;
for(let i = 0; i < hmacValue.length; i++)
{
Iif(hmacValue[i] !== dataView[i])
{
result = false;
break;
}
}
//endregion
return result;
}
//**************************************************************************************
module.exports.getRandomValues = getRandomValues;
module.exports.encryptUsingPBKDF1Password = encryptUsingPBKDF1Password;
module.exports.decryptUsingPBKDF1Password = decryptUsingPBKDF1Password;
module.exports.encryptUsingPBKDF2Password = encryptUsingPBKDF2Password;
module.exports.decryptUsingPBKDF2Password = decryptUsingPBKDF2Password;
module.exports.stampDataWithPassword = stampDataWithPassword;
module.exports.verifyDataStampedWithPassword = verifyDataStampedWithPassword;
//**************************************************************************************
|