Subversion Repositories cryptochat

Rev

View as "text/javascript" | Blame | Last modification | View Log | RSS feed

  1. /*
  2. CryptoJS v3.1.2
  3. code.google.com/p/crypto-js
  4. (c) 2009-2013 by Jeff Mott. All rights reserved.
  5. code.google.com/p/crypto-js/wiki/License
  6. */
  7. (function () {
  8.     // Shortcuts
  9.     var C = CryptoJS;
  10.     var C_lib = C.lib;
  11.     var Base = C_lib.Base;
  12.     var WordArray = C_lib.WordArray;
  13.     var C_algo = C.algo;
  14.     var SHA1 = C_algo.SHA1;
  15.     var HMAC = C_algo.HMAC;
  16.  
  17.     /**
  18.      * Password-Based Key Derivation Function 2 algorithm.
  19.      */
  20.     var PBKDF2 = C_algo.PBKDF2 = Base.extend({
  21.         /**
  22.          * Configuration options.
  23.          *
  24.          * @property {number} keySize The key size in words to generate. Default: 4 (128 bits)
  25.          * @property {Hasher} hasher The hasher to use. Default: SHA1
  26.          * @property {number} iterations The number of iterations to perform. Default: 1
  27.          */
  28.         cfg: Base.extend({
  29.             keySize: 128/32,
  30.             hasher: SHA1,
  31.             iterations: 1
  32.         }),
  33.  
  34.         /**
  35.          * Initializes a newly created key derivation function.
  36.          *
  37.          * @param {Object} cfg (Optional) The configuration options to use for the derivation.
  38.          *
  39.          * @example
  40.          *
  41.          *     var kdf = CryptoJS.algo.PBKDF2.create();
  42.          *     var kdf = CryptoJS.algo.PBKDF2.create({ keySize: 8 });
  43.          *     var kdf = CryptoJS.algo.PBKDF2.create({ keySize: 8, iterations: 1000 });
  44.          */
  45.         init: function (cfg) {
  46.             this.cfg = this.cfg.extend(cfg);
  47.         },
  48.  
  49.         /**
  50.          * Computes the Password-Based Key Derivation Function 2.
  51.          *
  52.          * @param {WordArray|string} password The password.
  53.          * @param {WordArray|string} salt A salt.
  54.          *
  55.          * @return {WordArray} The derived key.
  56.          *
  57.          * @example
  58.          *
  59.          *     var key = kdf.compute(password, salt);
  60.          */
  61.         compute: function (password, salt) {
  62.             // Shortcut
  63.             var cfg = this.cfg;
  64.  
  65.             // Init HMAC
  66.             var hmac = HMAC.create(cfg.hasher, password);
  67.  
  68.             // Initial values
  69.             var derivedKey = WordArray.create();
  70.             var blockIndex = WordArray.create([0x00000001]);
  71.  
  72.             // Shortcuts
  73.             var derivedKeyWords = derivedKey.words;
  74.             var blockIndexWords = blockIndex.words;
  75.             var keySize = cfg.keySize;
  76.             var iterations = cfg.iterations;
  77.  
  78.             // Generate key
  79.             while (derivedKeyWords.length < keySize) {
  80.                 var block = hmac.update(salt).finalize(blockIndex);
  81.                 hmac.reset();
  82.  
  83.                 // Shortcuts
  84.                 var blockWords = block.words;
  85.                 var blockWordsLength = blockWords.length;
  86.  
  87.                 // Iterations
  88.                 var intermediate = block;
  89.                 for (var i = 1; i < iterations; i++) {
  90.                     intermediate = hmac.finalize(intermediate);
  91.                     hmac.reset();
  92.  
  93.                     // Shortcut
  94.                     var intermediateWords = intermediate.words;
  95.  
  96.                     // XOR intermediate with block
  97.                     for (var j = 0; j < blockWordsLength; j++) {
  98.                         blockWords[j] ^= intermediateWords[j];
  99.                     }
  100.                 }
  101.  
  102.                 derivedKey.concat(block);
  103.                 blockIndexWords[0]++;
  104.             }
  105.             derivedKey.sigBytes = keySize * 4;
  106.  
  107.             return derivedKey;
  108.         }
  109.     });
  110.  
  111.     /**
  112.      * Computes the Password-Based Key Derivation Function 2.
  113.      *
  114.      * @param {WordArray|string} password The password.
  115.      * @param {WordArray|string} salt A salt.
  116.      * @param {Object} cfg (Optional) The configuration options to use for this computation.
  117.      *
  118.      * @return {WordArray} The derived key.
  119.      *
  120.      * @static
  121.      *
  122.      * @example
  123.      *
  124.      *     var key = CryptoJS.PBKDF2(password, salt);
  125.      *     var key = CryptoJS.PBKDF2(password, salt, { keySize: 8 });
  126.      *     var key = CryptoJS.PBKDF2(password, salt, { keySize: 8, iterations: 1000 });
  127.      */
  128.     C.PBKDF2 = function (password, salt, cfg) {
  129.         return PBKDF2.create(cfg).compute(password, salt);
  130.     };
  131. }());
  132.