Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <script src="http://peterolson.github.com/BigInteger.js/BigInteger.min.js"></script>
- <!-- Simple Password Hash Library -->
- <script>
- /*
- Calculates the value of (mantissa * (base^exponent)) and stores it into a bigInt
- */
- function bigInt_from_shorthand(mantissa, base, exponent)
- {
- return bigInt(mantissa).multiply(bigInt(base).pow(bigInt(exponent)));
- }
- /*
- See more large sophie-germain primes at http://yves.gallot.pagesperso-orange.fr/primes/chrrcds.html#Sophie
- */
- function safe_prime_from_sophie_germain_shorthand(mantissa, base, exponent, adjustment)
- {
- return bigInt(1).add(bigInt(2).multiply(bigInt(adjustment).add(bigInt_from_shorthand(mantissa, base, exponent))));
- }
- /*
- The final hash will essentially be the sum of two distinct values obtained from this function
- */
- function make_preliminary_hash_integer(value, first, second, modulus)
- {
- var temp = first.multiply(value).multiply(second);
- return temp.multiply(temp).mod(modulus);
- }
- /*
- Passing -1 as the truncate_length parameter truncates to the original text length, while zero means no truncation...
- */
- function make_safe_hash(text, truncate_length = 0)
- {
- text.trim();
- var length = text.length;
- /*
- The mapping may seem somewhat obscure, but just keep in mind that the point
- here is merely to create a number suitable for use with this particular hash function
- */
- var pad = "9";
- var digits = pad;
- for(var index = 0; index < length; ++index)
- {
- var data = text[index].charCodeAt();
- // The resulting number is more likely to be unique if each character generates a three-digit sequence
- if(data < 100)
- digits += pad;
- digits += data;
- }
- digits += pad;
- var value = bigInt(digits);
- if(typeof make_safe_hash.initialized === "undefined")
- {
- make_safe_hash.initialized = true;
- make_safe_hash.lhs =
- {
- first : safe_prime_from_sophie_germain_shorthand(72021, 2, 23630, -1),
- second : safe_prime_from_sophie_germain_shorthand(14516877, 2, 24176, -1),
- modulus : safe_prime_from_sophie_germain_shorthand(18458709, 2, 32611, -1)
- };
- make_safe_hash.rhs =
- {
- first : safe_prime_from_sophie_germain_shorthand(470943129, 2, 16352, -1),
- second : safe_prime_from_sophie_germain_shorthand(8069496435, 10, 5072, -1),
- modulus : safe_prime_from_sophie_germain_shorthand(2375063906985, 2, 19380, -1)
- };
- }
- var lhs = make_preliminary_hash_integer
- (
- value,
- make_safe_hash.lhs.first,
- make_safe_hash.lhs.second,
- make_safe_hash.lhs.modulus
- );
- var rhs = make_preliminary_hash_integer
- (
- value,
- make_safe_hash.rhs.first,
- make_safe_hash.rhs.second,
- make_safe_hash.rhs.modulus
- );
- var result = lhs.add(rhs).toString();
- if(truncate_length)
- result = result.substr(0, truncate_length < 0 ? length : truncate_length);
- return result;
- }
- /*
- Passing -1 as the truncate_length parameter truncates to the original text length, while zero means no truncation...
- */
- function make_user_hash(username, password, truncate_length = 0)
- {
- // Note: 0x1e was chosen for the simple fact that it represents the "record separator" code in ascii
- return make_safe_hash(username.trim() + "\u001e" + password.trim(), truncate_length);
- }
- </script>
- <!-- Test Program -->
- <script>
- function display()
- {
- var length_input = document.getElementById('length');
- var length = Number(length_input.value);
- if(isNaN(length) || !length)
- {
- length = 0;
- length_input.value = "(default)";
- }
- var username = document.getElementById('username').value;
- var password = document.getElementById('password').value;
- document.getElementById('output').innerHTML = make_user_hash(username, password, length).toString();
- }
- function toggle()
- {
- var input = document.getElementById('password');
- var button = document.getElementById('visibility');
- if(button.value == "show")
- {
- input.type = "text";
- button.value = "hide";
- }
- else
- {
- input.type = "password";
- button.value = "show";
- }
- }
- </script>
- </head>
- <body style = "background-color:#cddeee;color:#369;font-family:Arial,Helvetica,sans-serif;">
- <br/>
- <h1 align = "center">Composite Hash Generator</h1>
- <br/>
- <br/>
- <br/>
- <form onsubmit="display()" action="javascript:void(0)">
- <table align = "center" style = "border-spacing:15px;border-collapse:separate;">
- <tr>
- <td>
- Username (optional)
- </td>
- <td>
- Password
-
- <input type = "button" id = "visibility" value = "show" onclick = "toggle()"/>
- </td>
- <td>
- Length
- </td>
- </tr>
- <tr>
- <td>
- <input type = "text" id = "username" onfocus = "this.select()"/>
- </td>
- <td>
- <input type = "password" id = "password" onfocus = "this.select()"/>
- </td>
- <td>
- <input type = "text" id = "length" onfocus = "this.select()" value = "(default)"/>
- </td>
- <tr>
- <td></td>
- <td align="center">
- <input type = "submit" id = "generate" value = "Generate!"/>
- </td>
- <td></td>
- </tr>
- </table>
- </form>
- <p id="output" style = "word-wrap:break-word;"></p>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement