Get minimum password length from options
[skeletonkey.git] / core.js
1 /* Copyright (c) 2012 Robert Sesek <http://robert.sesek.com>
2 *
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
4 * of this software and associated documentation files (the "Software"), to
5 * deal in the Software without restriction, including without limitation the
6 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7 * sell copies of the Software, and to permit persons to whom the Software is
8 * furnished to do so, subject to the following conditions:
9 *
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19 * DEALINGS IN THE SOFTWARE.
20 */
21
22 (function main() {
23 if (typeof chrome !== 'undefined') {
24 // TODO: load the extension JS
25 } else {
26 // TODO: load the hosted JS
27 }
28
29 document.addEventListener('DOMContentLoaded', function() {
30 var controller = new SkeletonKey(document);
31 });
32 })();
33
34 /**
35 * SkeletonKey is view controller for generating secure passwords.
36 *
37 * @param {HTMLDocument} doc The document on which to operate.
38 */
39 var SkeletonKey = SkeletonKey || function(doc) {
40 this._master = doc.getElementById('master');
41 this._sitekey = doc.getElementById('sitekey');
42 this._username = doc.getElementById('username');
43 this._password = doc.getElementById('password');
44 this._generateButton = doc.getElementById('generate');
45
46 this._options = new SkeletonKeyOptions();
47
48 this._init();
49 };
50
51 /**
52 * The number of iterations to perform in PBKDF2.
53 * @const {int}
54 */
55 SkeletonKey.prototype.ITERATIONS = 1000;
56 /**
57 * The size of the key, in bytes.
58 * @const {int}
59 */
60 SkeletonKey.prototype.KEYSIZE = 256/32;
61
62 /**
63 * Initializes event handlers for the page.
64 * @private
65 */
66 SkeletonKey.prototype._init = function() {
67 this._generateButton.onclick = this._onGenerate.bind(this);
68
69 this._master.onkeyup = this._nextFieldInterceptor.bind(this);
70 this._sitekey.onkeyup = this._nextFieldInterceptor.bind(this);
71 this._username.onkeyup = this._nextFieldInterceptor.bind(this);
72
73 this._password.onclick = this._selectPassword.bind(this);
74 this._password.labels[0].onclick = this._selectPassword.bind(this);
75
76 this._initChromeExtension();
77
78 this._master.focus();
79 };
80
81 /**
82 * Event handler for generating a new password.
83 * @param {Event} e
84 * @private
85 */
86 SkeletonKey.prototype._onGenerate = function(e) {
87 var salt = this._username.value + '@' + this._sitekey.value;
88
89 // |key| is a WordArray of 32-bit words.
90 var key = CryptoJS.PBKDF2(this._master.value, salt,
91 {keySize: this.KEYSIZE, iterations: this.ITERATIONS});
92 var hexString = key.toString();
93 hexString = this._capitalizeKey(hexString);
94 this._password.value = hexString;
95 this._selectPassword();
96 };
97
98 /**
99 * Takes a HEX string and returns a mixed-case string.
100 * @param {string} key
101 * @return string
102 * @private
103 */
104 SkeletonKey.prototype._capitalizeKey = function(key) {
105 // |key| is too long for a decent password, so try and use the second half of
106 // it as the basis for capitalizing the key.
107 var capsSource = null;
108 var keyLength = key.length;
109 if (keyLength / 2 <= this._options.getMinimumPasswordLength()) {
110 capsSouce = key.substr(0, keyLength - this._options.getMinimumPasswordLength());
111 } else {
112 capsSource = key.substr(keyLength / 2);
113 }
114
115 if (!capsSource || capsSource.length < 1) {
116 return key;
117 }
118
119 key = key.substr(0, capsSource.length);
120 var capsSourceLength = capsSource.length;
121
122 var j = 0;
123 var newKey = "";
124 for (var i = 0; i < key.length; i++) {
125 var c = key.charCodeAt(i);
126 // If this is not a lowercase letter or there's no more source, skip.
127 if (c < 0x61 || c > 0x7A || j >= capsSourceLength) {
128 newKey += key[i];
129 continue;
130 }
131
132 var makeCap = capsSource.charCodeAt(j++) % 2;
133 if (makeCap)
134 newKey += String.fromCharCode(c - 0x20);
135 else
136 newKey += key[i];
137 }
138
139 return newKey;
140 };
141
142 /**
143 * Checks if the given key event is from the enter key and moves onto the next
144 * field or generates the password.
145 * @param {Event} e
146 * @private
147 */
148 SkeletonKey.prototype._nextFieldInterceptor = function(e) {
149 if (e.keyCode != 0xD)
150 return;
151
152 if (this._master.value == "") {
153 this._master.focus();
154 } else if (this._sitekey.value == "") {
155 this._sitekey.focus();
156 } else if (this._username.value == "") {
157 this._username.focus();
158 } else {
159 this._generateButton.click();
160 }
161 };
162
163 /**
164 * Selects the contents of the generated password.
165 * @private
166 */
167 SkeletonKey.prototype._selectPassword = function() {
168 this._password.focus();
169 this._password.select();
170 };
171
172 /**
173 * Initalizes the Chrome extension pieces if running inside chrome.
174 * @private
175 */
176 SkeletonKey.prototype._initChromeExtension = function() {
177 return;
178 if (typeof chrome == 'undefined' || typeof chrome.extension == 'undefined')
179 return;
180
181 // getCurrent is undefined for backround pages. Need content script.
182 chrome.tabs.getCurrent(function (tab) {
183 if (tab == null)
184 return;
185
186 var url = tab.url;
187 if (url == null || url == "")
188 return;
189
190 var siteKey = url.search(/https?:\/\/(www.?|login|accounts?)\.(.*)\.(com?|net|org|edu|biz|info)?.*/);
191 console.log(siteKey);
192 });
193 };