PDA

View Full Version : Passwordfield (with hashing) V0.1



JorisA
27 Jan 2008, 5:03 AM
I'm (trying to) implement a secure login system without ssl, so I need passwords to be hashed before they are sent. This is a simple field that allows just that.


Ext.namespace('Ext.ux', 'Ext.ux.form');

/**
* @class Ext.ux.form.HashField
* @extends Ext.form.TextField
* A password field with a hook that hashes the value, and stores the result in the hidden field. Usefull for login challenges.
* @constructor
* Creates a new Field
* @param {Object} config Configuration options
*/
Ext.ux.form.HashField = Ext.extend(Ext.form.TextField, {
/**
* The hash function itself, example:
* <pre><code>
var sha1 = Ext.ux.Crypto.SHA1;
var salt = "1235";

new Ext.FormPanel({
height: 100,
renderTo: document.body,
items: [{
xtype: 'textfield',
fieldLabel: 'Name',
name: 'username'
},{
xtype: 'hashfield',
fieldLabel: 'Password',
hiddenName: 'response',
hashFn: function(s){ return sha1.hash('salt;' +s); }
}]
});
</code></pre>
*/

hashFn: function (s) { return s },

inputType: 'password',

initComponent : function(){
Ext.ux.form.HashField.superclass.initComponent.call(this);

if(typeof(this.algorithm) == 'string') {
this.algorithm = Ext.ux.Crypto[this.algorithm];
}
},

// private
initEvents : function(){
Ext.ux.form.HashField.superclass.initEvents.call(this);

this.el.on("keyup", this.onKeyUp, this);
},

// private
onRender : function(ct, position){
Ext.ux.form.HashField.superclass.onRender.call(this, ct, position);
if(this.hiddenName){
this.hiddenField = this.el.insertSibling({tag:'input', type:'hidden', name: this.hiddenName, id: (this.hiddenId||this.hiddenName)},
'before', true);
this.hiddenField.value = '';

// prevent input submission
this.el.dom.removeAttribute('name');
}
if(Ext.isGecko){
this.el.dom.setAttribute('autocomplete', 'off');
}
},

// private
onEnable: function(){
Ext.ux.form.HashField.superclass.onEnable.apply(this, arguments);
if(this.hiddenField){
this.hiddenField.disabled = false;
}
},

// private
onDisable: function(){
Ext.ux.form.HashField.superclass.onDisable.apply(this, arguments);
if(this.hiddenField){
this.hiddenField.disabled = true;
}
},

// private
onKeyUp: function(e) {
if(this.hiddenField){
this.hiddenField.value = this.hashFn(this.getRawValue());
}
},

/**
* Clears any text/value currently set in the field
*/
clearValue : function(){
if(this.hiddenField){
this.hiddenField.value = '';
}
this.setRawValue('');
this.applyEmptyText();
this.value = '';
},

/**
* Sets the specified value into the field. If the value finds a match, the corresponding record text
* will be displayed in the field. If the value does not match the data value of an existing item,
* and the valueNotFoundText config option is defined, it will be displayed as the default field text.
* Otherwise the field will be blank (although the value will still be set).
* @param {String} value The value to match
*/
setValue : function(v){
if(this.hiddenField){
this.hiddenField.value = this.hashFn(v);
}
Ext.ux.form.HashField.superclass.setValue.call(this, v);
this.value = v;
}
});
Ext.reg('hashfield', Ext.ux.form.HashField);

stever
29 Jan 2008, 10:08 AM
You should look into this: http://assl.sullof.com/assl/