My favorites
|
Sign in
halfmask
An experimental approach to password masking with obfuscation.
Project Home
Downloads
Wiki
Issues
Source
Checkout
|
Browse
|
Changes
|
‹r3
r4
Source path:
svn
/
trunk
/
jquery.halfmask.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/**
* HalfMask - a new approach to password masking security
*
* @author Chris Dary <umbrae@gmail.com>
* @copyright Copyright (c) 2009 {@link http://arc90.com Arc90 Inc.}
* @license http://www.opensource.org/licenses/bsd-license.php
**/
(function($) {
$.halfmask = {
settings: {
halfMaskColors: ['#300', '#003'],
numHalfMaskInputs: 2,
letterSet: 'abcdefghijklmnopqrstuvwxyz',
fontFamily: '"Courier New", Courier, Monospace',
inputAttributes: ['accessKey','className','disabled','id','maxlength','name','size','style','tabIndex','title','value'], /* Only used for IE */
halfMaskInputStyles: {
position: 'absolute',
background: 'transparent',
borderColor: 'transparent',
opacity: '0.5',
fontWeight: 'bold'
}
}
};
$.fn.halfmask = function(settings) {
/**
* @var object Contains an associative array of all settings for halfmask.
**/
settings = $.extend({}, $.halfmask.settings, settings);
/**
* Thankfully provided by Ben Nadel:
* http://www.bennadel.com/blog/199-Ask-Ben-Creating-Large-Random-Strings-In-Javascript.htm
**/
var randRange = function(intFrom, intTo){
intFrom = Math.floor(intFrom);
intTo = Math.floor(intTo);
return Math.floor(intFrom + ((intTo - intFrom + 1) * Math.random()));
};
/**
* Mask an input with halfmask. The input must be of type password.
*
* Halfmask creates some dummy inputs that will be filled with garbage
* text with a reduced opacity so that, hopefully, prying eyes can't understand
* the text just typed, but the original author can.
*
* @param selector string A jquery capable selector, as defined here: http://docs.jquery.com/Selectors
* @return void
**/
return this.each(function() {
var $input, i;
var $this = $(this);
if(!$this.is('input[type="password"]'))
{
throw new Error('HalfMask may only be used on inputs of type password.');
}
/**
* Replace the password input with one of type text with the same attributes.
* This allows for graceful degradation, as you cannot change an input's type attribute directly.
**/
if($.browser.msie)
{
if($.browser.version.substr(0,1) < 7)
{
return false;
}
$input = $('<input type="text" />');
for(i = 0; i < settings.inputAttributes.length; i++)
{
if(typeof $this.attr(settings.inputAttributes[i]) != 'undefined')
{
$input.attr(settings.inputAttributes[i], $this.attr(settings.inputAttributes[i]));
}
}
}
else
{
$input = $this.clone().attr( {
type: 'text',
spellcheck: false,
autocomplete: 'off'
});
}
$this.before($input).remove();
/**
* Save the inputs original dimensions, as changing fonts can
* alter them if they are determined by the size attribute.
**/
var inputDimensions = {width: $input.width(), height: $input.height()};
$input.css({
fontFamily: settings.fontFamily,
width: inputDimensions.width,
height: inputDimensions.height
});
var halfMaskInputStyles = $.extend({
top: $input.offset().top,
left: $input.offset().left,
fontFamily: settings.fontFamily
}, settings.halfMaskInputStyles);
var halfMaskInputs = [];
var focusOnInput = function() { $input.focus(); };
for(i = 0; i < settings.numHalfMaskInputs; i++)
{
var halfMaskInput = $input.clone().attr({
readonly: 'readonly',
autocomplete: 'off',
tabindex: '99999',
id: ($input.attr('id') + '-halfmask-' + i),
name: ($input.attr('name') + '-halfmask-' + i)
});
halfMaskInput.css($.extend(halfMaskInputStyles, { color: settings.halfMaskColors[i % settings.halfMaskColors.length] }));
halfMaskInput.click( focusOnInput );
halfMaskInputs.push(halfMaskInput);
$input.after(halfMaskInput);
}
$input.keydown(function(e) {
/**
* Add random characters to our masks to obfuscate the cleartext 'just enough'
* so that prying eyes can't understand it.
**/
var inputVal = $input.val();
var keyCode = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
for(var i = 0; i < halfMaskInputs.length; i++)
{
var halfMaskInput = halfMaskInputs[i];
if(keyCode < 47 && keyCode != 32 && keyCode !== 0)
{
return;
}
halfMaskInput.val(halfMaskInput.val().substr(0, inputVal.length) + settings.letterSet.charAt(randRange(0, settings.letterSet.length-1)));
}
}).keyup(function(e) {
/**
* Handle any deletions here by resizing our masks.
**/
for(var i = 0; i < halfMaskInputs.length; i++)
{
var halfMaskInput = halfMaskInputs[i];
halfMaskInput.val(halfMaskInput.val().substr(0, $input.val().length));
}
});
});
};
})(jQuery);
Show details
Hide details
Change log
r4
by umbrae on Jul 08, 2009
Diff
JSLinting, Tabs to spaces
Go to:
/trunk/jquery.halfmask.js
Project members,
sign in
to write a code review
Older revisions
r3
by umbrae on Jul 08, 2009
Diff
Tabs to spaces
r2
by umbrae on Jul 08, 2009
Diff
Initial commit
All revisions of this file
File info
Size: 6285 bytes, 168 lines
View raw file
Hosted by