MD5 for sencha touch

3rd July 2014 by Ian Martin

MD5+for+sencha+touch Image

MD5 for sencha touch

3rd July 2014 by Ian Martin

There's no native MD5 functionality in JavaScript, but Joseph Meyers has written his own function, which accoring to this stackoverflow thread is the fastest one in town! It didn't take much work, but I've converted it into a utility class, ready to drop into your Sencha Touch projects.


Ext.define('MyApp.util.Crypto', {

singleton : true,

alternateClassName : ['Crypto'],



constructor: function(config) {

this.initConfig(config);

},



md5cycle: function(x, k) {

var me = this;

var a = x[0], b = x[1], c = x[2], d = x[3];



a = me.ff(a, b, c, d, k[0], 7, -680876936);

d = me.ff(d, a, b, c, k[1], 12, -389564586);

c = me.ff(c, d, a, b, k[2], 17,  606105819);

b = me.ff(b, c, d, a, k[3], 22, -1044525330);

a = me.ff(a, b, c, d, k[4], 7, -176418897);

d = me.ff(d, a, b, c, k[5], 12,  1200080426);

c = me.ff(c, d, a, b, k[6], 17, -1473231341);

b = me.ff(b, c, d, a, k[7], 22, -45705983);

a = me.ff(a, b, c, d, k[8], 7,  1770035416);

d = me.ff(d, a, b, c, k[9], 12, -1958414417);

c = me.ff(c, d, a, b, k[10], 17, -42063);

b = me.ff(b, c, d, a, k[11], 22, -1990404162);

a = me.ff(a, b, c, d, k[12], 7,  1804603682);

d = me.ff(d, a, b, c, k[13], 12, -40341101);

c = me.ff(c, d, a, b, k[14], 17, -1502002290);

b = me.ff(b, c, d, a, k[15], 22,  1236535329);



a = me.gg(a, b, c, d, k[1], 5, -165796510);

d = me.gg(d, a, b, c, k[6], 9, -1069501632);

c = me.gg(c, d, a, b, k[11], 14,  643717713);

b = me.gg(b, c, d, a, k[0], 20, -373897302);

a = me.gg(a, b, c, d, k[5], 5, -701558691);

d = me.gg(d, a, b, c, k[10], 9,  38016083);

c = me.gg(c, d, a, b, k[15], 14, -660478335);

b = me.gg(b, c, d, a, k[4], 20, -405537848);

a = me.gg(a, b, c, d, k[9], 5,  568446438);

d = me.gg(d, a, b, c, k[14], 9, -1019803690);

c = me.gg(c, d, a, b, k[3], 14, -187363961);

b = me.gg(b, c, d, a, k[8], 20,  1163531501);

a = me.gg(a, b, c, d, k[13], 5, -1444681467);

d = me.gg(d, a, b, c, k[2], 9, -51403784);

c = me.gg(c, d, a, b, k[7], 14,  1735328473);

b = me.gg(b, c, d, a, k[12], 20, -1926607734);



a = me.hh(a, b, c, d, k[5], 4, -378558);

d = me.hh(d, a, b, c, k[8], 11, -2022574463);

c = me.hh(c, d, a, b, k[11], 16,  1839030562);

b = me.hh(b, c, d, a, k[14], 23, -35309556);

a = me.hh(a, b, c, d, k[1], 4, -1530992060);

d = me.hh(d, a, b, c, k[4], 11,  1272893353);

c = me.hh(c, d, a, b, k[7], 16, -155497632);

b = me.hh(b, c, d, a, k[10], 23, -1094730640);

a = me.hh(a, b, c, d, k[13], 4,  681279174);

d = me.hh(d, a, b, c, k[0], 11, -358537222);

c = me.hh(c, d, a, b, k[3], 16, -722521979);

b = me.hh(b, c, d, a, k[6], 23,  76029189);

a = me.hh(a, b, c, d, k[9], 4, -640364487);

d = me.hh(d, a, b, c, k[12], 11, -421815835);

c = me.hh(c, d, a, b, k[15], 16,  530742520);

b = me.hh(b, c, d, a, k[2], 23, -995338651);



a = me.ii(a, b, c, d, k[0], 6, -198630844);

d = me.ii(d, a, b, c, k[7], 10,  1126891415);

c = me.ii(c, d, a, b, k[14], 15, -1416354905);

b = me.ii(b, c, d, a, k[5], 21, -57434055);

a = me.ii(a, b, c, d, k[12], 6,  1700485571);

d = me.ii(d, a, b, c, k[3], 10, -1894986606);

c = me.ii(c, d, a, b, k[10], 15, -1051523);

b = me.ii(b, c, d, a, k[1], 21, -2054922799);

a = me.ii(a, b, c, d, k[8], 6,  1873313359);

d = me.ii(d, a, b, c, k[15], 10, -30611744);

c = me.ii(c, d, a, b, k[6], 15, -1560198380);

b = me.ii(b, c, d, a, k[13], 21,  1309151649);

a = me.ii(a, b, c, d, k[4], 6, -145523070);

d = me.ii(d, a, b, c, k[11], 10, -1120210379);

c = me.ii(c, d, a, b, k[2], 15,  718787259);

b = me.ii(b, c, d, a, k[9], 21, -343485551);



x[0] = me.add32(a, x[0]);

x[1] = me.add32(b, x[1]);

x[2] = me.add32(c, x[2]);

x[3] = me.add32(d, x[3]);



},



cmn: function(q, a, b, x, s, t) {

var me = this;

a = me.add32(me.add32(a, q), me.add32(x, t));

return me.add32((a << s) | (a >>> (32 - s)), b);

},



ff: function (a, b, c, d, x, s, t) {

return this.cmn((b & c) | ((~b) & d), a, b, x, s, t);

},



gg: function(a, b, c, d, x, s, t) {

return this.cmn((b & d) | (c & (~d)), a, b, x, s, t);

},



hh: function(a, b, c, d, x, s, t) {

return this.cmn(b^c^d, a, b, x, s, t);

},



ii: function(a, b, c, d, x, s, t) {

return this.cmn(c^(b | (~d)), a, b, x, s, t);

},



md51: function(s) {

var me = this;

txt = '';

var n = s.length,

state = [1732584193, -271733879, -1732584194, 271733878],

i;

for (i = 64; i <= s.length; i += 64) {

me.md5cycle(state, me.md5blk(s.substring(i - 64, i)));

}

s = s.substring(i - 64);

var tail = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];

for (i = 0; i < s.length; i++)

tail[i >> 2] |= s.charCodeAt(i) << ((i % 4) << 3);

tail[i >> 2] |= 0x80 << ((i % 4) << 3);

if (i > 55) {

me.md5cycle(state, tail);

for (i = 0; i < 16; i++)

tail[i] = 0;

}

tail[14] = n * 8;

me.md5cycle(state, tail);

return state;

},



md5blk: function(s) {

var md5blks = [],

i;

for (i = 0; i < 64; i += 4) {

md5blks[i >> 2] = s.charCodeAt(i)

+ (s.charCodeAt(i + 1) << 8 )

+ (s.charCodeAt(i + 2) << 16)

+ (s.charCodeAt(i + 3) << 24);

}

return md5blks;

},



hex_chr: '0123456789abcdef'.split(''),



rhex: function(n) {

var s = '',

j = 0;

for (; j < 4; j++)

s += this.hex_chr[(n >> (j * 8 + 4)) & 0x0F]

+ this.hex_chr[(n >> (j * 8)) & 0x0F];

return s;

},



hex: function(x) {

for (var i = 0; i < x.length; i++)

x[i] = this.rhex(x[i]);

return x.join('');

},



md5: function(s) {

return this.hex(this.md51(s));

},



add32: function(a, b) {

return (a + b) & 0xFFFFFFFF;

}



})



Include in app.js



requires: [ 'MyApp.util.Crypto']



Use like this:



alert(Crypto.md5('hello'));