packages/ssoinabox-webui/root/usr/local/share/ssoinabox/htdocs/res/md5.js
author Dan Fuhry <dan@enanocms.org>
Sat, 23 Feb 2013 14:26:38 -0500
changeset 9 f4bf6556fb9f
parent 4 2212b2ded8bf
permissions -rw-r--r--
Merged
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
     1
/*
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
     2
 * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
     3
 * Digest Algorithm, as defined in RFC 1321.
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
     4
 * Version 2.2 Copyright (C) Paul Johnston 1999 - 2009
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
     5
 * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
     6
 * Distributed under the BSD License
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
     7
 * See http://pajhome.org.uk/crypt/md5 for more info.
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
     8
 */
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
     9
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    10
/*
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    11
 * Configurable variables. You may need to tweak these to be compatible with
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    12
 * the server-side, but the defaults work in most cases.
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    13
 */
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    14
var hexcase = 0;   /* hex output format. 0 - lowercase; 1 - uppercase        */
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    15
var b64pad  = "";  /* base-64 pad character. "=" for strict RFC compliance   */
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    16
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    17
/*
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    18
 * These are the functions you'll usually want to call
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    19
 * They take string arguments and return either hex or base-64 encoded strings
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    20
 */
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    21
function hex_md5(s)    { return rstr2hex(rstr_md5(str2rstr_utf8(s))); }
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    22
function b64_md5(s)    { return rstr2b64(rstr_md5(str2rstr_utf8(s))); }
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    23
function any_md5(s, e) { return rstr2any(rstr_md5(str2rstr_utf8(s)), e); }
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    24
function hex_hmac_md5(k, d)
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    25
  { return rstr2hex(rstr_hmac_md5(str2rstr_utf8(k), str2rstr_utf8(d))); }
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    26
function b64_hmac_md5(k, d)
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    27
  { return rstr2b64(rstr_hmac_md5(str2rstr_utf8(k), str2rstr_utf8(d))); }
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    28
function any_hmac_md5(k, d, e)
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    29
  { return rstr2any(rstr_hmac_md5(str2rstr_utf8(k), str2rstr_utf8(d)), e); }
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    30
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    31
/*
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    32
 * Perform a simple self-test to see if the VM is working
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    33
 */
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    34
function md5_vm_test()
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    35
{
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    36
  return hex_md5("abc").toLowerCase() == "900150983cd24fb0d6963f7d28e17f72";
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    37
}
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    38
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    39
/*
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    40
 * Calculate the MD5 of a raw string
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    41
 */
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    42
function rstr_md5(s)
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    43
{
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    44
  return binl2rstr(binl_md5(rstr2binl(s), s.length * 8));
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    45
}
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    46
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    47
/*
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    48
 * Calculate the HMAC-MD5, of a key and some data (raw strings)
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    49
 */
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    50
function rstr_hmac_md5(key, data)
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    51
{
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    52
  var bkey = rstr2binl(key);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    53
  if(bkey.length > 16) bkey = binl_md5(bkey, key.length * 8);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    54
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    55
  var ipad = Array(16), opad = Array(16);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    56
  for(var i = 0; i < 16; i++)
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    57
  {
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    58
    ipad[i] = bkey[i] ^ 0x36363636;
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    59
    opad[i] = bkey[i] ^ 0x5C5C5C5C;
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    60
  }
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    61
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    62
  var hash = binl_md5(ipad.concat(rstr2binl(data)), 512 + data.length * 8);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    63
  return binl2rstr(binl_md5(opad.concat(hash), 512 + 128));
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    64
}
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    65
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    66
/*
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    67
 * Convert a raw string to a hex string
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    68
 */
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    69
function rstr2hex(input)
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    70
{
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    71
  try { hexcase } catch(e) { hexcase=0; }
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    72
  var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    73
  var output = "";
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    74
  var x;
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    75
  for(var i = 0; i < input.length; i++)
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    76
  {
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    77
    x = input.charCodeAt(i);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    78
    output += hex_tab.charAt((x >>> 4) & 0x0F)
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    79
           +  hex_tab.charAt( x        & 0x0F);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    80
  }
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    81
  return output;
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    82
}
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    83
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    84
/*
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    85
 * Convert a raw string to a base-64 string
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    86
 */
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    87
function rstr2b64(input)
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    88
{
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    89
  try { b64pad } catch(e) { b64pad=''; }
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    90
  var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    91
  var output = "";
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    92
  var len = input.length;
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    93
  for(var i = 0; i < len; i += 3)
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    94
  {
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    95
    var triplet = (input.charCodeAt(i) << 16)
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    96
                | (i + 1 < len ? input.charCodeAt(i+1) << 8 : 0)
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    97
                | (i + 2 < len ? input.charCodeAt(i+2)      : 0);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    98
    for(var j = 0; j < 4; j++)
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
    99
    {
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   100
      if(i * 8 + j * 6 > input.length * 8) output += b64pad;
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   101
      else output += tab.charAt((triplet >>> 6*(3-j)) & 0x3F);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   102
    }
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   103
  }
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   104
  return output;
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   105
}
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   106
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   107
/*
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   108
 * Convert a raw string to an arbitrary string encoding
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   109
 */
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   110
function rstr2any(input, encoding)
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   111
{
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   112
  var divisor = encoding.length;
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   113
  var i, j, q, x, quotient;
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   114
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   115
  /* Convert to an array of 16-bit big-endian values, forming the dividend */
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   116
  var dividend = Array(Math.ceil(input.length / 2));
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   117
  for(i = 0; i < dividend.length; i++)
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   118
  {
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   119
    dividend[i] = (input.charCodeAt(i * 2) << 8) | input.charCodeAt(i * 2 + 1);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   120
  }
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   121
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   122
  /*
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   123
   * Repeatedly perform a long division. The binary array forms the dividend,
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   124
   * the length of the encoding is the divisor. Once computed, the quotient
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   125
   * forms the dividend for the next step. All remainders are stored for later
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   126
   * use.
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   127
   */
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   128
  var full_length = Math.ceil(input.length * 8 /
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   129
                                    (Math.log(encoding.length) / Math.log(2)));
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   130
  var remainders = Array(full_length);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   131
  for(j = 0; j < full_length; j++)
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   132
  {
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   133
    quotient = Array();
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   134
    x = 0;
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   135
    for(i = 0; i < dividend.length; i++)
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   136
    {
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   137
      x = (x << 16) + dividend[i];
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   138
      q = Math.floor(x / divisor);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   139
      x -= q * divisor;
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   140
      if(quotient.length > 0 || q > 0)
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   141
        quotient[quotient.length] = q;
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   142
    }
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   143
    remainders[j] = x;
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   144
    dividend = quotient;
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   145
  }
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   146
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   147
  /* Convert the remainders to the output string */
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   148
  var output = "";
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   149
  for(i = remainders.length - 1; i >= 0; i--)
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   150
    output += encoding.charAt(remainders[i]);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   151
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   152
  return output;
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   153
}
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   154
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   155
/*
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   156
 * Encode a string as utf-8.
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   157
 * For efficiency, this assumes the input is valid utf-16.
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   158
 */
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   159
function str2rstr_utf8(input)
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   160
{
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   161
  var output = "";
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   162
  var i = -1;
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   163
  var x, y;
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   164
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   165
  while(++i < input.length)
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   166
  {
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   167
    /* Decode utf-16 surrogate pairs */
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   168
    x = input.charCodeAt(i);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   169
    y = i + 1 < input.length ? input.charCodeAt(i + 1) : 0;
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   170
    if(0xD800 <= x && x <= 0xDBFF && 0xDC00 <= y && y <= 0xDFFF)
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   171
    {
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   172
      x = 0x10000 + ((x & 0x03FF) << 10) + (y & 0x03FF);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   173
      i++;
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   174
    }
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   175
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   176
    /* Encode output as utf-8 */
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   177
    if(x <= 0x7F)
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   178
      output += String.fromCharCode(x);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   179
    else if(x <= 0x7FF)
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   180
      output += String.fromCharCode(0xC0 | ((x >>> 6 ) & 0x1F),
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   181
                                    0x80 | ( x         & 0x3F));
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   182
    else if(x <= 0xFFFF)
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   183
      output += String.fromCharCode(0xE0 | ((x >>> 12) & 0x0F),
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   184
                                    0x80 | ((x >>> 6 ) & 0x3F),
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   185
                                    0x80 | ( x         & 0x3F));
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   186
    else if(x <= 0x1FFFFF)
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   187
      output += String.fromCharCode(0xF0 | ((x >>> 18) & 0x07),
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   188
                                    0x80 | ((x >>> 12) & 0x3F),
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   189
                                    0x80 | ((x >>> 6 ) & 0x3F),
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   190
                                    0x80 | ( x         & 0x3F));
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   191
  }
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   192
  return output;
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   193
}
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   194
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   195
/*
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   196
 * Encode a string as utf-16
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   197
 */
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   198
function str2rstr_utf16le(input)
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   199
{
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   200
  var output = "";
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   201
  for(var i = 0; i < input.length; i++)
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   202
    output += String.fromCharCode( input.charCodeAt(i)        & 0xFF,
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   203
                                  (input.charCodeAt(i) >>> 8) & 0xFF);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   204
  return output;
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   205
}
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   206
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   207
function str2rstr_utf16be(input)
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   208
{
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   209
  var output = "";
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   210
  for(var i = 0; i < input.length; i++)
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   211
    output += String.fromCharCode((input.charCodeAt(i) >>> 8) & 0xFF,
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   212
                                   input.charCodeAt(i)        & 0xFF);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   213
  return output;
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   214
}
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   215
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   216
/*
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   217
 * Convert a raw string to an array of little-endian words
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   218
 * Characters >255 have their high-byte silently ignored.
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   219
 */
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   220
function rstr2binl(input)
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   221
{
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   222
  var output = Array(input.length >> 2);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   223
  for(var i = 0; i < output.length; i++)
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   224
    output[i] = 0;
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   225
  for(var i = 0; i < input.length * 8; i += 8)
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   226
    output[i>>5] |= (input.charCodeAt(i / 8) & 0xFF) << (i%32);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   227
  return output;
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   228
}
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   229
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   230
/*
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   231
 * Convert an array of little-endian words to a string
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   232
 */
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   233
function binl2rstr(input)
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   234
{
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   235
  var output = "";
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   236
  for(var i = 0; i < input.length * 32; i += 8)
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   237
    output += String.fromCharCode((input[i>>5] >>> (i % 32)) & 0xFF);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   238
  return output;
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   239
}
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   240
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   241
/*
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   242
 * Calculate the MD5 of an array of little-endian words, and a bit length.
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   243
 */
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   244
function binl_md5(x, len)
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   245
{
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   246
  /* append padding */
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   247
  x[len >> 5] |= 0x80 << ((len) % 32);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   248
  x[(((len + 64) >>> 9) << 4) + 14] = len;
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   249
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   250
  var a =  1732584193;
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   251
  var b = -271733879;
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   252
  var c = -1732584194;
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   253
  var d =  271733878;
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   254
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   255
  for(var i = 0; i < x.length; i += 16)
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   256
  {
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   257
    var olda = a;
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   258
    var oldb = b;
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   259
    var oldc = c;
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   260
    var oldd = d;
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   261
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   262
    a = md5_ff(a, b, c, d, x[i+ 0], 7 , -680876936);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   263
    d = md5_ff(d, a, b, c, x[i+ 1], 12, -389564586);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   264
    c = md5_ff(c, d, a, b, x[i+ 2], 17,  606105819);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   265
    b = md5_ff(b, c, d, a, x[i+ 3], 22, -1044525330);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   266
    a = md5_ff(a, b, c, d, x[i+ 4], 7 , -176418897);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   267
    d = md5_ff(d, a, b, c, x[i+ 5], 12,  1200080426);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   268
    c = md5_ff(c, d, a, b, x[i+ 6], 17, -1473231341);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   269
    b = md5_ff(b, c, d, a, x[i+ 7], 22, -45705983);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   270
    a = md5_ff(a, b, c, d, x[i+ 8], 7 ,  1770035416);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   271
    d = md5_ff(d, a, b, c, x[i+ 9], 12, -1958414417);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   272
    c = md5_ff(c, d, a, b, x[i+10], 17, -42063);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   273
    b = md5_ff(b, c, d, a, x[i+11], 22, -1990404162);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   274
    a = md5_ff(a, b, c, d, x[i+12], 7 ,  1804603682);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   275
    d = md5_ff(d, a, b, c, x[i+13], 12, -40341101);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   276
    c = md5_ff(c, d, a, b, x[i+14], 17, -1502002290);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   277
    b = md5_ff(b, c, d, a, x[i+15], 22,  1236535329);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   278
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   279
    a = md5_gg(a, b, c, d, x[i+ 1], 5 , -165796510);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   280
    d = md5_gg(d, a, b, c, x[i+ 6], 9 , -1069501632);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   281
    c = md5_gg(c, d, a, b, x[i+11], 14,  643717713);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   282
    b = md5_gg(b, c, d, a, x[i+ 0], 20, -373897302);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   283
    a = md5_gg(a, b, c, d, x[i+ 5], 5 , -701558691);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   284
    d = md5_gg(d, a, b, c, x[i+10], 9 ,  38016083);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   285
    c = md5_gg(c, d, a, b, x[i+15], 14, -660478335);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   286
    b = md5_gg(b, c, d, a, x[i+ 4], 20, -405537848);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   287
    a = md5_gg(a, b, c, d, x[i+ 9], 5 ,  568446438);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   288
    d = md5_gg(d, a, b, c, x[i+14], 9 , -1019803690);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   289
    c = md5_gg(c, d, a, b, x[i+ 3], 14, -187363961);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   290
    b = md5_gg(b, c, d, a, x[i+ 8], 20,  1163531501);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   291
    a = md5_gg(a, b, c, d, x[i+13], 5 , -1444681467);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   292
    d = md5_gg(d, a, b, c, x[i+ 2], 9 , -51403784);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   293
    c = md5_gg(c, d, a, b, x[i+ 7], 14,  1735328473);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   294
    b = md5_gg(b, c, d, a, x[i+12], 20, -1926607734);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   295
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   296
    a = md5_hh(a, b, c, d, x[i+ 5], 4 , -378558);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   297
    d = md5_hh(d, a, b, c, x[i+ 8], 11, -2022574463);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   298
    c = md5_hh(c, d, a, b, x[i+11], 16,  1839030562);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   299
    b = md5_hh(b, c, d, a, x[i+14], 23, -35309556);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   300
    a = md5_hh(a, b, c, d, x[i+ 1], 4 , -1530992060);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   301
    d = md5_hh(d, a, b, c, x[i+ 4], 11,  1272893353);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   302
    c = md5_hh(c, d, a, b, x[i+ 7], 16, -155497632);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   303
    b = md5_hh(b, c, d, a, x[i+10], 23, -1094730640);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   304
    a = md5_hh(a, b, c, d, x[i+13], 4 ,  681279174);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   305
    d = md5_hh(d, a, b, c, x[i+ 0], 11, -358537222);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   306
    c = md5_hh(c, d, a, b, x[i+ 3], 16, -722521979);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   307
    b = md5_hh(b, c, d, a, x[i+ 6], 23,  76029189);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   308
    a = md5_hh(a, b, c, d, x[i+ 9], 4 , -640364487);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   309
    d = md5_hh(d, a, b, c, x[i+12], 11, -421815835);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   310
    c = md5_hh(c, d, a, b, x[i+15], 16,  530742520);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   311
    b = md5_hh(b, c, d, a, x[i+ 2], 23, -995338651);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   312
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   313
    a = md5_ii(a, b, c, d, x[i+ 0], 6 , -198630844);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   314
    d = md5_ii(d, a, b, c, x[i+ 7], 10,  1126891415);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   315
    c = md5_ii(c, d, a, b, x[i+14], 15, -1416354905);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   316
    b = md5_ii(b, c, d, a, x[i+ 5], 21, -57434055);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   317
    a = md5_ii(a, b, c, d, x[i+12], 6 ,  1700485571);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   318
    d = md5_ii(d, a, b, c, x[i+ 3], 10, -1894986606);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   319
    c = md5_ii(c, d, a, b, x[i+10], 15, -1051523);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   320
    b = md5_ii(b, c, d, a, x[i+ 1], 21, -2054922799);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   321
    a = md5_ii(a, b, c, d, x[i+ 8], 6 ,  1873313359);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   322
    d = md5_ii(d, a, b, c, x[i+15], 10, -30611744);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   323
    c = md5_ii(c, d, a, b, x[i+ 6], 15, -1560198380);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   324
    b = md5_ii(b, c, d, a, x[i+13], 21,  1309151649);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   325
    a = md5_ii(a, b, c, d, x[i+ 4], 6 , -145523070);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   326
    d = md5_ii(d, a, b, c, x[i+11], 10, -1120210379);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   327
    c = md5_ii(c, d, a, b, x[i+ 2], 15,  718787259);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   328
    b = md5_ii(b, c, d, a, x[i+ 9], 21, -343485551);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   329
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   330
    a = safe_add(a, olda);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   331
    b = safe_add(b, oldb);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   332
    c = safe_add(c, oldc);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   333
    d = safe_add(d, oldd);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   334
  }
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   335
  return Array(a, b, c, d);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   336
}
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   337
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   338
/*
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   339
 * These functions implement the four basic operations the algorithm uses.
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   340
 */
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   341
function md5_cmn(q, a, b, x, s, t)
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   342
{
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   343
  return safe_add(bit_rol(safe_add(safe_add(a, q), safe_add(x, t)), s),b);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   344
}
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   345
function md5_ff(a, b, c, d, x, s, t)
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   346
{
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   347
  return md5_cmn((b & c) | ((~b) & d), a, b, x, s, t);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   348
}
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   349
function md5_gg(a, b, c, d, x, s, t)
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   350
{
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   351
  return md5_cmn((b & d) | (c & (~d)), a, b, x, s, t);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   352
}
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   353
function md5_hh(a, b, c, d, x, s, t)
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   354
{
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   355
  return md5_cmn(b ^ c ^ d, a, b, x, s, t);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   356
}
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   357
function md5_ii(a, b, c, d, x, s, t)
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   358
{
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   359
  return md5_cmn(c ^ (b | (~d)), a, b, x, s, t);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   360
}
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   361
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   362
/*
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   363
 * Add integers, wrapping at 2^32. This uses 16-bit operations internally
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   364
 * to work around bugs in some JS interpreters.
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   365
 */
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   366
function safe_add(x, y)
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   367
{
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   368
  var lsw = (x & 0xFFFF) + (y & 0xFFFF);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   369
  var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   370
  return (msw << 16) | (lsw & 0xFFFF);
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   371
}
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   372
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   373
/*
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   374
 * Bitwise rotate a 32-bit number to the left.
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   375
 */
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   376
function bit_rol(num, cnt)
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   377
{
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   378
  return (num << cnt) | (num >>> (32 - cnt));
2212b2ded8bf Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff changeset
   379
}