things kinda work now. switched sha1 implementations. probably not necessary, but...
authorOwen Smith <owen@omsmith.ca>
Wed, 21 Nov 2012 01:22:42 +0000 (20:22 -0500)
committerOwen Smith <owen@omsmith.ca>
Wed, 21 Nov 2012 01:22:42 +0000 (20:22 -0500)
www/assets/js/client.js
www/assets/js/jquery.sha1.js [deleted file]
www/assets/js/sha1.js [new file with mode: 0644]
www/index.html

index b029416..8ad33b0 100644 (file)
@@ -1,42 +1,42 @@
 PosClientWrapper = function (authKey, serverLocation) {
-       var _authKey = authKey;
+       var _authKey = 'balls';
 
-       var _transport = new Thrift.Transport(serverLocation);
-       var _protocol = new Thrift.Protocol(this._transport);
-       var _client = new PosBookieClient(this._protocol);
+       var _transport = new Thrift.Transport('http://home.omsmith.ca/api/');
+       var _protocol = new Thrift.Protocol(_transport);
+       var _client = new PosBookieClient(_protocol);
 
        var _getAuthString = function () {
-               var salt = this._client.getSalt();
+               var salt = _client.getSalt();
 
-               return $.sha1(salt+_authKey);
+               return Sha1.hash(salt+_authKey);
        }
 
+       this.getPrice = function (upc, callback) {
+               if (callback === undefined) {
+                       return _client.getUPCPrice(upc) / 100;
+               }
 
-       /*      try {
-               this._client.ping();
-       } catch (e) {
-               location.reload(true);
-       }*/
-};
-
-PosClientWrapper.prototype = {};
-
-PosClientWrapper.prototype.getPrice = function (upc, callback) {
-       if (callback === undefined) {
-               return this._client.getUPCPrice(upc);
+               _client.getUPCPrice(upc, function (p) {
+                       callback(p/100);
+               });
        }
 
-       _client.getUPCPrice(upc, callback);
-}
+       this.process = function (cardData, selection) {
+               var upcs = [];
 
-PosClientWrapper.prototype.process = function (cardData, selection) {
-       var upcs = [];
-
-       for (var i in selection) {
-               for (var q = 0; q < selection[i].quantity; q++) {
-                       upcs.push(i);
+               for (var i in selection) {
+                       for (var q = 0; q < selection[i].quantity; q++) {
+                               upcs.push(i);
+                       }
                }
+
+               return _client.purchaseItems(_getAuthString(), cardData, upcs);
        }
 
-       return _client.purchase(_getAuthString(), cardData, upcs);
-}
+       try {
+               _transport.open();
+               _client.ping();
+       } catch (e) {
+               location.reload(true);
+       }
+};
diff --git a/www/assets/js/jquery.sha1.js b/www/assets/js/jquery.sha1.js
deleted file mode 100644 (file)
index 40c5725..0000000
+++ /dev/null
@@ -1,170 +0,0 @@
-
-/**
- * jQuery SHA1 hash algorithm function
- * 
- *     <code>
- *             Calculate the sha1 hash of a String 
- *             String $.sha1 ( String str )
- *     </code>
- * 
- * Calculates the sha1 hash of str using the US Secure Hash Algorithm 1.
- * SHA-1 the Secure Hash Algorithm (SHA) was developed by NIST and is specified in the Secure Hash Standard (SHS, FIPS 180).
- * This script is used to process variable length message into a fixed-length output using the SHA-1 algorithm. It is fully compatible with UTF-8 encoding.
- * If you plan using UTF-8 encoding in your project don't forget to set the page encoding to UTF-8 (Content-Type meta tag).
- * This function orginally get from the WebToolkit and rewrite for using as the jQuery plugin.
- * 
- * Example
- *     Code
- *             <code>
- *                     $.sha1("I'm Persian."); 
- *             </code>
- *     Result
- *             <code>
- *                     "1d302f9dc925d62fc859055999d2052e274513ed"
- *             </code>
- * 
- * @alias Muhammad Hussein Fattahizadeh < muhammad [AT] semnanweb [DOT] com >
- * @link http://www.semnanweb.com/jquery-plugin/sha1.html
- * @see http://www.webtoolkit.info/
- * @license http://www.gnu.org/licenses/gpl.html [GNU General Public License]
- * @param {jQuery} {sha1:function(string))
- * @return string
- */
-
-(function($){
-       
-       var rotateLeft = function(lValue, iShiftBits) {
-               return (lValue << iShiftBits) | (lValue >>> (32 - iShiftBits));
-       }
-       
-       var lsbHex = function(value) {
-               var string = "";
-               var i;
-               var vh;
-               var vl;
-               for(i = 0;i <= 6;i += 2) {
-                       vh = (value>>>(i * 4 + 4))&0x0f;
-                       vl = (value>>>(i*4))&0x0f;
-                       string += vh.toString(16) + vl.toString(16);
-               }
-               return string;
-       };
-       
-       var cvtHex = function(value) {
-               var string = "";
-               var i;
-               var v;
-               for(i = 7;i >= 0;i--) {
-                       v = (value>>>(i * 4))&0x0f;
-                       string += v.toString(16);
-               }
-               return string;
-       };
-       
-       var uTF8Encode = function(string) {
-               string = string.replace(/\x0d\x0a/g, "\x0a");
-               var output = "";
-               for (var n = 0; n < string.length; n++) {
-                       var c = string.charCodeAt(n);
-                       if (c < 128) {
-                               output += String.fromCharCode(c);
-                       } else if ((c > 127) && (c < 2048)) {
-                               output += String.fromCharCode((c >> 6) | 192);
-                               output += String.fromCharCode((c & 63) | 128);
-                       } else {
-                               output += String.fromCharCode((c >> 12) | 224);
-                               output += String.fromCharCode(((c >> 6) & 63) | 128);
-                               output += String.fromCharCode((c & 63) | 128);
-                       }
-               }
-               return output;
-       };
-       
-       $.extend({
-               sha1: function(string) {
-                       var blockstart;
-                       var i, j;
-                       var W = new Array(80);
-                       var H0 = 0x67452301;
-                       var H1 = 0xEFCDAB89;
-                       var H2 = 0x98BADCFE;
-                       var H3 = 0x10325476;
-                       var H4 = 0xC3D2E1F0;
-                       var A, B, C, D, E;
-                       var tempValue;
-                       string = uTF8Encode(string);
-                       var stringLength = string.length;
-                       var wordArray = new Array();
-                       for(i = 0;i < stringLength - 3;i += 4) {
-                               j = string.charCodeAt(i)<<24 | string.charCodeAt(i + 1)<<16 | string.charCodeAt(i + 2)<<8 | string.charCodeAt(i + 3);
-                               wordArray.push(j);
-                       }
-                       switch(stringLength % 4) {
-                               case 0:
-                                       i = 0x080000000;
-                               break;
-                               case 1:
-                                       i = string.charCodeAt(stringLength - 1)<<24 | 0x0800000;
-                               break;
-                               case 2:
-                                       i = string.charCodeAt(stringLength - 2)<<24 | string.charCodeAt(stringLength - 1)<<16 | 0x08000;
-                               break;
-                               case 3:
-                                       i = string.charCodeAt(stringLength - 3)<<24 | string.charCodeAt(stringLength - 2)<<16 | string.charCodeAt(stringLength - 1)<<8 | 0x80;
-                               break;
-                       }
-                       wordArray.push(i);
-                       while((wordArray.length % 16) != 14 ) wordArray.push(0);
-                       wordArray.push(stringLength>>>29);
-                       wordArray.push((stringLength<<3)&0x0ffffffff);
-                       for(blockstart = 0;blockstart < wordArray.length;blockstart += 16) {
-                               for(i = 0;i < 16;i++) W[i] = wordArray[blockstart+i];
-                               for(i = 16;i <= 79;i++) W[i] = rotateLeft(W[i-3] ^ W[i-8] ^ W[i-14] ^ W[i-16], 1);
-                               A = H0;
-                               B = H1;
-                               C = H2;
-                               D = H3;
-                               E = H4;
-                               for(i = 0;i <= 19;i++) {
-                                       tempValue = (rotateLeft(A, 5) + ((B&C) | (~B&D)) + E + W[i] + 0x5A827999) & 0x0ffffffff;
-                                       E = D;
-                                       D = C;
-                                       C = rotateLeft(B, 30);
-                                       B = A;
-                                       A = tempValue;
-                               }
-                               for(i = 20;i <= 39;i++) {
-                                       tempValue = (rotateLeft(A, 5) + (B ^ C ^ D) + E + W[i] + 0x6ED9EBA1) & 0x0ffffffff;
-                                       E = D;
-                                       D = C;
-                                       C = rotateLeft(B, 30);
-                                       B = A;
-                                       A = tempValue;
-                               }
-                               for(i = 40;i <= 59;i++) {
-                                       tempValue = (rotateLeft(A, 5) + ((B&C) | (B&D) | (C&D)) + E + W[i] + 0x8F1BBCDC) & 0x0ffffffff;
-                                       E = D;
-                                       D = C;
-                                       C = rotateLeft(B, 30);
-                                       B = A;
-                                       A = tempValue;
-                               }
-                               for(i = 60;i <= 79;i++) {
-                                       tempValue = (rotateLeft(A, 5) + (B ^ C ^ D) + E + W[i] + 0xCA62C1D6) & 0x0ffffffff;
-                                       E = D;
-                                       D = C;
-                                       C = rotateLeft(B, 30);
-                                       B = A;
-                                       A = tempValue;
-                               }
-                               H0 = (H0 + A) & 0x0ffffffff;
-                               H1 = (H1 + B) & 0x0ffffffff;
-                               H2 = (H2 + C) & 0x0ffffffff;
-                               H3 = (H3 + D) & 0x0ffffffff;
-                               H4 = (H4 + E) & 0x0ffffffff;
-                       }
-                       var tempValue = cvtHex(H0) + cvtHex(H1) + cvtHex(H2) + cvtHex(H3) + cvtHex(H4);
-                       return tempValue.toLowerCase();
-               }
-       });
-})(jQuery);
\ No newline at end of file
diff --git a/www/assets/js/sha1.js b/www/assets/js/sha1.js
new file mode 100644 (file)
index 0000000..0801819
--- /dev/null
@@ -0,0 +1,177 @@
+/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  */
+/*  SHA-1 implementation in JavaScript | (c) Chris Veness 2002-2010 | www.movable-type.co.uk      */
+/*   - see http://csrc.nist.gov/groups/ST/toolkit/secure_hashing.html                             */
+/*         http://csrc.nist.gov/groups/ST/toolkit/examples.html                                   */
+/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  */
+
+var Sha1 = {};  // Sha1 namespace
+
+/**
+ * Generates SHA-1 hash of string
+ *
+ * @param {String} msg                String to be hashed
+ * @param {Boolean} [utf8encode=true] Encode msg as UTF-8 before generating hash
+ * @returns {String}                  Hash of msg as hex character string
+ */
+Sha1.hash = function(msg, utf8encode) {
+  utf8encode =  (typeof utf8encode == 'undefined') ? true : utf8encode;
+  
+  // convert string to UTF-8, as SHA only deals with byte-streams
+  if (utf8encode) msg = Utf8.encode(msg);
+  
+  // constants [§4.2.1]
+  var K = [0x5a827999, 0x6ed9eba1, 0x8f1bbcdc, 0xca62c1d6];
+  
+  // PREPROCESSING 
+  
+  msg += String.fromCharCode(0x80);  // add trailing '1' bit (+ 0's padding) to string [§5.1.1]
+  
+  // convert string msg into 512-bit/16-integer blocks arrays of ints [§5.2.1]
+  var l = msg.length/4 + 2;  // length (in 32-bit integers) of msg + ‘1’ + appended length
+  var N = Math.ceil(l/16);   // number of 16-integer-blocks required to hold 'l' ints
+  var M = new Array(N);
+  
+  for (var i=0; i<N; i++) {
+    M[i] = new Array(16);
+    for (var j=0; j<16; j++) {  // encode 4 chars per integer, big-endian encoding
+      M[i][j] = (msg.charCodeAt(i*64+j*4)<<24) | (msg.charCodeAt(i*64+j*4+1)<<16) | 
+        (msg.charCodeAt(i*64+j*4+2)<<8) | (msg.charCodeAt(i*64+j*4+3));
+    } // note running off the end of msg is ok 'cos bitwise ops on NaN return 0
+  }
+  // add length (in bits) into final pair of 32-bit integers (big-endian) [§5.1.1]
+  // note: most significant word would be (len-1)*8 >>> 32, but since JS converts
+  // bitwise-op args to 32 bits, we need to simulate this by arithmetic operators
+  M[N-1][14] = ((msg.length-1)*8) / Math.pow(2, 32); M[N-1][14] = Math.floor(M[N-1][14])
+  M[N-1][15] = ((msg.length-1)*8) & 0xffffffff;
+  
+  // set initial hash value [§5.3.1]
+  var H0 = 0x67452301;
+  var H1 = 0xefcdab89;
+  var H2 = 0x98badcfe;
+  var H3 = 0x10325476;
+  var H4 = 0xc3d2e1f0;
+  
+  // HASH COMPUTATION [§6.1.2]
+  
+  var W = new Array(80); var a, b, c, d, e;
+  for (var i=0; i<N; i++) {
+  
+    // 1 - prepare message schedule 'W'
+    for (var t=0;  t<16; t++) W[t] = M[i][t];
+    for (var t=16; t<80; t++) W[t] = Sha1.ROTL(W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16], 1);
+    
+    // 2 - initialise five working variables a, b, c, d, e with previous hash value
+    a = H0; b = H1; c = H2; d = H3; e = H4;
+    
+    // 3 - main loop
+    for (var t=0; t<80; t++) {
+      var s = Math.floor(t/20); // seq for blocks of 'f' functions and 'K' constants
+      var T = (Sha1.ROTL(a,5) + Sha1.f(s,b,c,d) + e + K[s] + W[t]) & 0xffffffff;
+      e = d;
+      d = c;
+      c = Sha1.ROTL(b, 30);
+      b = a;
+      a = T;
+    }
+    
+    // 4 - compute the new intermediate hash value
+    H0 = (H0+a) & 0xffffffff;  // note 'addition modulo 2^32'
+    H1 = (H1+b) & 0xffffffff; 
+    H2 = (H2+c) & 0xffffffff; 
+    H3 = (H3+d) & 0xffffffff; 
+    H4 = (H4+e) & 0xffffffff;
+  }
+
+  return Sha1.toHexStr(H0) + Sha1.toHexStr(H1) + 
+    Sha1.toHexStr(H2) + Sha1.toHexStr(H3) + Sha1.toHexStr(H4);
+}
+
+//
+// function 'f' [§4.1.1]
+//
+Sha1.f = function(s, x, y, z)  {
+  switch (s) {
+  case 0: return (x & y) ^ (~x & z);           // Ch()
+  case 1: return x ^ y ^ z;                    // Parity()
+  case 2: return (x & y) ^ (x & z) ^ (y & z);  // Maj()
+  case 3: return x ^ y ^ z;                    // Parity()
+  }
+}
+
+//
+// rotate left (circular left shift) value x by n positions [§3.2.5]
+//
+Sha1.ROTL = function(x, n) {
+  return (x<<n) | (x>>>(32-n));
+}
+
+//
+// hexadecimal representation of a number 
+//   (note toString(16) is implementation-dependant, and  
+//   in IE returns signed numbers when used on full words)
+//
+Sha1.toHexStr = function(n) {
+  var s="", v;
+  for (var i=7; i>=0; i--) { v = (n>>>(i*4)) & 0xf; s += v.toString(16); }
+  return s;
+}
+
+
+/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  */
+/*  Utf8 class: encode / decode between multi-byte Unicode characters and UTF-8 multiple          */
+/*              single-byte character encoding (c) Chris Veness 2002-2010                         */
+/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  */
+
+var Utf8 = {};  // Utf8 namespace
+
+/**
+ * Encode multi-byte Unicode string into utf-8 multiple single-byte characters 
+ * (BMP / basic multilingual plane only)
+ *
+ * Chars in range U+0080 - U+07FF are encoded in 2 chars, U+0800 - U+FFFF in 3 chars
+ *
+ * @param {String} strUni Unicode string to be encoded as UTF-8
+ * @returns {String} encoded string
+ */
+Utf8.encode = function(strUni) {
+  // use regular expressions & String.replace callback function for better efficiency 
+  // than procedural approaches
+  var strUtf = strUni.replace(
+      /[\u0080-\u07ff]/g,  // U+0080 - U+07FF => 2 bytes 110yyyyy, 10zzzzzz
+      function(c) { 
+        var cc = c.charCodeAt(0);
+        return String.fromCharCode(0xc0 | cc>>6, 0x80 | cc&0x3f); }
+    );
+  strUtf = strUtf.replace(
+      /[\u0800-\uffff]/g,  // U+0800 - U+FFFF => 3 bytes 1110xxxx, 10yyyyyy, 10zzzzzz
+      function(c) { 
+        var cc = c.charCodeAt(0); 
+        return String.fromCharCode(0xe0 | cc>>12, 0x80 | cc>>6&0x3F, 0x80 | cc&0x3f); }
+    );
+  return strUtf;
+}
+
+/**
+ * Decode utf-8 encoded string back into multi-byte Unicode characters
+ *
+ * @param {String} strUtf UTF-8 string to be decoded back to Unicode
+ * @returns {String} decoded string
+ */
+Utf8.decode = function(strUtf) {
+  // note: decode 3-byte chars first as decoded 2-byte strings could appear to be 3-byte char!
+  var strUni = strUtf.replace(
+      /[\u00e0-\u00ef][\u0080-\u00bf][\u0080-\u00bf]/g,  // 3-byte chars
+      function(c) {  // (note parentheses for precence)
+        var cc = ((c.charCodeAt(0)&0x0f)<<12) | ((c.charCodeAt(1)&0x3f)<<6) | ( c.charCodeAt(2)&0x3f); 
+        return String.fromCharCode(cc); }
+    );
+  strUni = strUni.replace(
+      /[\u00c0-\u00df][\u0080-\u00bf]/g,                 // 2-byte chars
+      function(c) {  // (note parentheses for precence)
+        var cc = (c.charCodeAt(0)&0x1f)<<6 | c.charCodeAt(1)&0x3f;
+        return String.fromCharCode(cc); }
+    );
+  return strUni;
+}
+
+/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  */
index c9f6fb1..219c9bd 100644 (file)
@@ -75,8 +75,8 @@
 
                <!-- js included here -->
                <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
-               <script src="assets/js/jquery.sha1.js"></script>
                <script src="assets/js/jquery.formatCurrency-1.4.0.min.js"></script>
+               <script src="assets/js/sha1.js"></script>
                <script src="assets/js/bootstrap.min.js"></script>
                <script src="assets/js/thrift.js"></script>
                <script src="assets/js/pos-bookie_types.js"></script>