
// This script and many more are available free online at
// The JavaScript Source!! http://javascript.internet.com

/* Are the JavaScripts here free?  What's the catch?                       */
/* Yes, we offer our site as a free "public service" resource.  Simply     */
/* stated, all the scripts on this site are free for personal or business  */
/* use.  The only requirement for use of archives is that you leave the    */
/* credit information inside the script.  We also don't think it's too     */
/* much to ask if you would please link back to The JavaScript Source site */
/* to help spread the word about our site.  The address for linking to The */
/* JavaScript Source is:                                                   */
/*                                                                         */
/* http://javascript.internet.com                                          */
/*                                                                         */
/* Can I remove the "credit" line in the JavaScripts?                      */
/* We are not going to say that you can not remove them, but we would      */
/* really appreciate if you left them in.  I do not think it is too much   */
/* to ask that you leave them intact, especially since we do not require   */
/* any sort of payment for the scripts in The JavaScript Source.           */
/*                                                                         */
/************************************************************************
* Copyright 2001 by Terry Yuen.
* Email: kaiser40@yahoo.com
* Last update: July 15, 2001.
*************************************************************************/

function mix(str, pwd) {
  if(pwd == null || pwd.length <= 0) {
    return null;
  }
  var prand = "";
  for(var i=0; i<pwd.length; i++) {
    prand += pwd.charCodeAt(i).toString();
  }
  var sPos = Math.floor(prand.length / 5);
  var mult = parseInt(prand.charAt(sPos) + prand.charAt(sPos*2) + prand.charAt(sPos*3) + prand.charAt(sPos*4) + prand.charAt(sPos*5));
  var incr = Math.ceil(pwd.length / 2);
  var modu = Math.pow(2, 31) - 1;
  if(mult < 2) {
    alert("hash failure (" + str + "/" + pwd + ")")
    return null;
  }
  var salt = Math.round(Math.random() * 1000000000) % 100000000;
  prand += salt;
  while(prand.length > 10) {
    prand = (parseInt(prand.substring(0, 10)) + parseInt(prand.substring(10, prand.length))).toString();
  }
  prand = (mult * prand + incr) % modu;
  var enc_chr = "";
  var enc_str = "";
  for(var i=0; i<str.length; i++) {
    enc_chr = parseInt(str.charCodeAt(i) ^ Math.floor((prand / modu) * 255));
    if(enc_chr < 16) {
      enc_str += "0" + enc_chr.toString(16);
    } else enc_str += enc_chr.toString(16);
    prand = (mult * prand + incr) % modu;
  }
  salt = salt.toString(16);
  while(salt.length < 8)salt = "0" + salt;
  enc_str += salt;
  return enc_str;
}

function unmix(str, pwd) {
  if(str == null || str.length < 8) {
  //alert("msg too short.") // JFK must do error checking
    return;
  }
  if(pwd == null || pwd.length <= 0) {
    return;
  }
  var prand = "";
  for(var i=0; i<pwd.length; i++) {
    prand += pwd.charCodeAt(i).toString();
  }
  var sPos = Math.floor(prand.length / 5);
  var mult = parseInt(prand.charAt(sPos) + prand.charAt(sPos*2) + prand.charAt(sPos*3) + prand.charAt(sPos*4) + prand.charAt(sPos*5));
  var incr = Math.round(pwd.length / 2);
  var modu = Math.pow(2, 31) - 1;
  var salt = parseInt(str.substring(str.length - 8, str.length), 16);
  str = str.substring(0, str.length - 8);
  prand += salt;
  while(prand.length > 10) {
    prand = (parseInt(prand.substring(0, 10)) + parseInt(prand.substring(10, prand.length))).toString();
  }
  prand = (mult * prand + incr) % modu;
  var enc_chr = "";
  var enc_str = "";
  for(var i=0; i<str.length; i+=2) {
    enc_chr = parseInt(parseInt(str.substring(i, i+2), 16) ^ Math.floor((prand / modu) * 255));
    enc_str += String.fromCharCode(enc_chr);
    prand = (mult * prand + incr) % modu;
  }
  return enc_str;
}
