/* Cryptnos Online Core Library Primary Author: Jeffrey T. Darlington Last Updated: March 22, 2011 This JavaScript library provides the core functionality of Cryptnos Online aside from the actual cryptographic hashes themselves. It takes care of form generation, input validation, and performing the core task of generating the user's password from the supplied inputs. The goal is to minimize the mixing of JavaScript and HTML, so that the HTML page need only include a minimum of embedded JavaScript. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ var Cryptnos = {}; /* Highlight the given form element: */ Cryptnos.HighlightBox = function(theBox) { theBox.style.background = 'maroon'; theBox.style.color = 'white'; theBox.focus(); }; /* Unhighlight the given form element: */ Cryptnos.UnhighlightBox = function(theBox) { theBox.style.background = 'black'; theBox.style.color = '#C0C0C0'; }; /* Given a hash algorithm name, return the length of the Base64-encoded result- ing string. This is used by VerifyLength() below to set the maximum length value the user can specify. */ Cryptnos.MaxHashLength = function(hash) { /* This isn't particularly elegant; all I did was compute the hash of a file using the specified algorithm, count the number of characters, and hard-code it. However, I thought that was more efficient than getting that number programmatically. Note that if we get an unexpected result, we return a -1. */ switch (hash) { case 'MD5': return 24; case 'SHA-1': return 28; case 'SHA-256': return 44; case 'SHA-512': return 88; case 'RIPEMD-160': return 28; default: return -1; } }; /* Adjust the character limit restrictions options when a new hash algorithm is selected. */ Cryptnos.ChangeHash = function(hashBox) { /* Get the hash algorithm and its maximum length: */ var newHash = hashBox.value; var newMaxLength = Cryptnos.MaxHashLength(newHash); /* Get the currently selected limit by getting the currently selected option: */ var currCharLimit = Cryptnos.TheForm.charLimit.selectedIndex; var i = 0; /* If the new hash is a valid one: */ if (newMaxLength > 0) { /* This probably isn't the most efficient way of doing things, but it seems to be what works. Rather than dynamically adding and removing items from the list, we're going to be destructive and blow away the entire character limit option array and recreate it every time the hash changes. Fortunatley, there aren't a lot of values in this drop-down, so it shouldn't take very long. I absolutely *hate* doing browser-specific code, but IE 7 just doesn't want to play along. While the code below works fine in all the other browsers, it doesn't work for IE 7, so we'll have to test for that separately. */ var ie7regex = /MSIE 7/; if (ie7regex.test(navigator.userAgent)) { Cryptnos.TheForm.charLimit.options.length = 0; Cryptnos.TheForm.charLimit.options[0] = new Option("None", 0, true, false); for (i = 1; i <= newMaxLength; i++) { Cryptnos.TheForm.charLimit.options[i] = new Option(i, i, false, false); } /* I'm not happy with this code either, but the main reason I went this route was that Safari didn't like the code above. Fortunately, this seems to work for everyone else except IE 7 (it even works with IE 8) so it looks like the more compatible version. It's a bit weird, but hey, this is JavaScript. *Everything* is weird. */ } else { /* Taken from the following URL: http://www.webmasters.am/blog/javascript-select-add-remove-methods-in-safari-bug-fix/javascript/2009/10/ We've wrapped the "); document.writeln("Master Password:"); document.writeln("Hash Algorithm:"); /* SHA-384, Whirlpool, and Tiger are supported by the other clients, but we don't have JavaScript versions of them yet. Once we do, we'll add them to the hash drop-down. */ // document.write("Hash Iterations: iterations"); document.writeln("Character Restriction:Use "); document.write("Length Restriction:"); document.writeln("Generate Password:"); document.writeln("Generated Password:"); document.writeln(""); /* Set the reference to the form, now that it's been built: */ Cryptnos.TheForm = document.forms.cryptnos.elements; }; /* For browsers we know we have specific problems with, tell the caller to display a warning that the browser is incompatible with Cryptnos. */ Cryptnos.IsBrowserCompatible = function() { /* We don't even *care* if you're running IE 6: */ var ie6regex = /MSIE 6/; if (ie6regex.test(navigator.userAgent)) { return false; } /* The Nintendo Wii doesn't work: */ var wiiregex = /Wii/; if (wiiregex.test(navigator.userAgent)) { return false; } /* Everything else will default to true unless we have reason to specifically warn the user about it. */ return true; }; /* This contains a reference to the form DOM, which we reference everywhere above. */ Cryptnos.TheForm = null;