/*(c) 2006 - 2008 AllenPort Co. All Rights Reserved.
All versions of this code, including the source and executable versions, 
constitute the intellectual property of AllenPort Co., which expressly reserves 
any and all U.S. and foreign rights and benefits to the code under copyright, 
trade secret and any other intellectual property law or international treaty 
whatsoever. Use of the code is subject to the terms and conditions of a separate 
written license agreement, and the code shall not be reproduced, modified, distributed 
or otherwise used in any form or manner whatsoever without obtaining the prior written 
permission of AllenPort Co. Any unauthorized reproduction or distribution of the code, 
or any portion of it, may result in civil and criminal penalties and be prosecuted 
to the fullest extent of the law.*/
//Pad ~ |pw_lengh|salt|password|Crc|
// password should be string
// function returns padded string
function make_pad(password, padd_to_size)
{	
	//result in padded_string_buffer
	var padded_string_buffer = "";
	
	//All in bytes
	var length_len = 1;
	var password_len = password.length;
	var crc_len = 4; //(32 бита)
	
	if(padd_to_size == 0)
		padd_to_size = password_len + length_len + crc_len;
  		
	var salt_size = padd_to_size - password_len - length_len - crc_len;	
  		
	padded_string_buffer += String.fromCharCode(password_len);
  		
	var rng = new SecureRandom();
	var rand_number = new Array(salt_size);

  rand_number.length = salt_size;
  rng.nextBytes(rand_number);
  var salt_block = "";
  for (bt in rand_number)   {
  			salt_block += String.fromCharCode(rand_number[bt]);
 	}
  padded_string_buffer += salt_block;
  padded_string_buffer += password;
  		    
  var crc_data = gen_crc(password);
  
  var b0 = crc_data & 0xff;
  crc_data >>= 8;
  var b1 = crc_data & 0xff;
  crc_data >>= 8;
  var b2 = crc_data & 0xff;
  crc_data >>= 8;
  var b3 = crc_data & 0xff;

  padded_string_buffer += String.fromCharCode(b0) + String.fromCharCode(b1) + String.fromCharCode(b2) + String.fromCharCode(b3);
	return padded_string_buffer;
}
//out: 32-bit integer crc number of data
function gen_crc(data)
{
	var a_ = 0;
	var b_ = 0;
	
	for(var i = 0; i < data.length; i++)
	{
		 a_ = a_ + data.charCodeAt(i);
		 a_ &= 0xffff;
		 b_ = b_ + a_; 
		 b_ &= 0xffff;
	}
	return (a_ | (b_ << 16));
}

function un_pad(padded_string)
{
	var length = padded_string.charCodeAt(0);

	var b0 = padded_string.charCodeAt(padded_string.length-4);
	var b1 = padded_string.charCodeAt(padded_string.length-3);
	var b2 = padded_string.charCodeAt(padded_string.length-2);
	var b3 = padded_string.charCodeAt(padded_string.length-1);
	
	var crc_data = b3;
	crc_data <<= 8;
	crc_data |= b2;
	crc_data <<= 8;
	crc_data |= b1;
	crc_data <<= 8;
	crc_data |= b0;

	var source_string = padded_string.substring(padded_string.length - length - 4, padded_string.length - 4);
	var crc = gen_crc(source_string);

	if(crc != crc_data)
	  return "";


  return source_string;
}
