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 MD5 = C_algo.MD5;
  15.  
  16.     /**
  17.      * This key derivation function is meant to conform with EVP_BytesToKey.
  18.      * www.openssl.org/docs/crypto/EVP_BytesToKey.html
  19.      */
  20.     var EvpKDF = C_algo.EvpKDF = 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 hash algorithm to use. Default: MD5
  26.          * @property {number} iterations The number of iterations to perform. Default: 1
  27.          */
  28.         cfg: Base.extend({
  29.             keySize: 128/32,
  30.             hasher: MD5,
  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.EvpKDF.create();
  42.          *     var kdf = CryptoJS.algo.EvpKDF.create({ keySize: 8 });
  43.          *     var kdf = CryptoJS.algo.EvpKDF.create({ keySize: 8, iterations: 1000 });
  44.          */
  45.         init: function (cfg) {
  46.             this.cfg = this.cfg.extend(cfg);
  47.         },
  48.  
  49.         /**
  50.          * Derives a key from a password.
  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 hasher
  66.             var hasher = cfg.hasher.create();
  67.  
  68.             // Initial values
  69.             var derivedKey = WordArray.create();
  70.  
  71.             // Shortcuts
  72.             var derivedKeyWords = derivedKey.words;
  73.             var keySize = cfg.keySize;
  74.             var iterations = cfg.iterations;
  75.  
  76.             // Generate key
  77.             while (derivedKeyWords.length < keySize) {
  78.                 if (block) {
  79.                     hasher.update(block);
  80.                 }
  81.                 var block = hasher.update(password).finalize(salt);
  82.                 hasher.reset();
  83.  
  84.                 // Iterations
  85.                 for (var i = 1; i < iterations; i++) {
  86.                     block = hasher.finalize(block);
  87.                     hasher.reset();
  88.                 }
  89.  
  90.                 derivedKey.concat(block);
  91.             }
  92.             derivedKey.sigBytes = keySize * 4;
  93.  
  94.             return derivedKey;
  95.         }
  96.     });
  97.  
  98.     /**
  99.      * Derives a key from a password.
  100.      *
  101.      * @param {WordArray|string} password The password.
  102.      * @param {WordArray|string} salt A salt.
  103.      * @param {Object} cfg (Optional) The configuration options to use for this computation.
  104.      *
  105.      * @return {WordArray} The derived key.
  106.      *
  107.      * @static
  108.      *
  109.      * @example
  110.      *
  111.      *     var key = CryptoJS.EvpKDF(password, salt);
  112.      *     var key = CryptoJS.EvpKDF(password, salt, { keySize: 8 });
  113.      *     var key = CryptoJS.EvpKDF(password, salt, { keySize: 8, iterations: 1000 });
  114.      */
  115.     C.EvpKDF = function (password, salt, cfg) {
  116.         return EvpKDF.create(cfg).compute(password, salt);
  117.     };
  118. }());
  119.