/* * A WML Script implementation of the Secure Hash Algorithm, SHA-1 * as defined in FIPS PUB 180-1 * Digest Algorithm, as defined in RFC 1321. * Copyright (C) Simon Wistow, 200 */ /* * Convert a 32-bit number to a hex string with ms-byte first */ use url ordlib "ord.wmls"; function hex(num) { var hex_chr = "0123456789abcdef"; var str = ""; for(var j = 7; j >= 0; j--) str += String.charAt(hex_chr, num >> (j * 4) & 0x0F); return str; } /* * Convert a string to a sequence of 16-word blocks, stored as an array. * Append padding bits and the length, as described in the SHA1 standard. */ function str2blks_SHA1(str) { var nblk = ((String.length(str) + 8) >> 6) + 1; var blks = "0"; var ord_str = ordlib#ord_string(str); for(var i = 1; i < nblk * 16; i++) blks += '|0'; for(i = 0; i <= String.length(str); i++) { var temp = String.elementAt(blks, i>>2, '|'); if (i != String.length(str)) temp |= String.charAt(ord_str,i) << (24 - (i % 4) * 8); else temp |= 0x80 << (24 - (i % 4) * 8); blks = String.replaceAt(blks, temp, i>>2, '|'); } blks = String.replaceAt(blks, String.length(str) * 8, nblk * 16 - 1, '|'); return blks; } /* * Add integers, wrapping at 2^32 */ function add(x, y) { return ((x&0x7FFFFFFF) + (y&0x7FFFFFFF)) ^ (x&0x80000000) ^ (y&0x80000000); } /* * Bitwise rotate a 32-bit number to the left */ function rol(num, cnt) { return (num << cnt) | (num >>> (32 - cnt)); } /* * Perform the appropriate triplet combination function for the current * iteration */ function ft(t, b, c, d) { if(t < 20) return (b & c) | ((~b) & d); if(t < 40) return b ^ c ^ d; if(t < 60) return (b & c) | (b & d) | (c & d); return b ^ c ^ d; } /* * Determine the appropriate additive constant for the current iteration */ function kt(t) { if(t < 20) return 0x5A827999; if(t < 40) return 0x6ED9EBA1; if(t < 60) return 0x8F1BBCDC; return 0xCA62C1D6; } /* * Take a string and return the hex representation of its SHA-1. */ function calcSHA1(str) { var x = str2blks_SHA1(str); var w = "0"; for (var k=0; k<79; k++) { w += "|0"; } var a = 0x67452301; var b = 0xEFCDAB89; var c = 0x98BADCFE; var d = 0x10325476; var e = 0xC3D2E1F0; for(var i = 0; i < String.elements(x,'|'); i += 16) { var olda = a; var oldb = b; var oldc = c; var oldd = d; var olde = e; for(var j = 0; j < 80; j++) { if(j < 16) w = String.replaceAt(w,String.elementAt(x,i + j,'|'),j,'|'); else w= String.replaceAt(w,rol(String.elementAt(w,j-3,'|') ^ String.elementAt(w,j-8,'|') ^ String.elementAt(w,j-14,'|') ^ String.elementAt(w,j-16,'|'), 1),j,'|'); var t = add(add(rol(a, 5), ft(j, b, c, d)), add(add(e, String.elementAt(w,j,'|')), kt(j))); e = d; d = c; c = rol(b, 30); b = a; a = t; } a = add(a, olda); b = add(b, oldb); c = add(c, oldc); d = add(d, oldd); e = add(e, olde); } return hex(a) + hex(b) + hex(c) + hex(d) + hex(e); } extern function hash (var_to_set, string) { WMLBrowser.setVar(var_to_set, calcSHA1(URL.unescapeString(string))); WMLBrowser.refresh (); }