a3a733520de03c214f7f701c9027c0dff29490ef.svn-base 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <head>
  4. <script type="text/javascript"
  5. src="../../../../dojo/dojo.js" djConfig="isDebug: false"></script>
  6. <script type="text/javascript" src="../../../../dojox/off/offline.js"></script>
  7. <style type="text/css">
  8. body{
  9. padding: 2em;
  10. }
  11. #dataTable{
  12. margin-top: 2em;
  13. }
  14. button{
  15. margin-left: 1em;
  16. }
  17. th, tr, td{
  18. text-align: left;
  19. }
  20. table{
  21. text-align: center;
  22. clear: both;
  23. }
  24. #cryptoContainer{
  25. float: left;
  26. width: 60%;
  27. }
  28. #numRowsContainer{
  29. float: right;
  30. width: 40%;
  31. }
  32. #numRowsContainer input{
  33. margin-left: 1.5em;
  34. width: 5em;
  35. }
  36. .table-columns{
  37. font-weight: bold;
  38. }
  39. </style>
  40. <script>
  41. dojo.require("dojox.sql");
  42. dojo.connect(window, "onload", function(){
  43. // draw our customer table on the screen
  44. createTable();
  45. // create our customer table in the database
  46. dojox.sql("DROP TABLE IF EXISTS CUSTOMERS");
  47. dojox.sql("CREATE TABLE CUSTOMERS ("
  48. + "last_name TEXT, "
  49. + "first_name TEXT, "
  50. + "social_security TEXT"
  51. + ")"
  52. );
  53. });
  54. function createTable(){
  55. // get number of rows to create
  56. var NUM_ROWS = document.getElementById("numRows").value;
  57. if(!NUM_ROWS){
  58. alert("Please enter the number of "
  59. + "customer rows the table should have");
  60. return;
  61. }
  62. var table = document.getElementById("dataTable");
  63. if(table){
  64. table.parentNode.removeChild(table);
  65. }
  66. table = document.createElement("table");
  67. table.setAttribute("id", "dataTable");
  68. table.setAttribute("border", 1);
  69. // if we don't use IE's craptacular proprietary table methods
  70. // we get strange display glitches
  71. var tr = (dojo.isIE) ? table.insertRow() : document.createElement("tr");
  72. tr.className = "table-columns";
  73. var th = (dojo.isIE) ? tr.insertCell() : document.createElement("th");
  74. th.appendChild(document.createTextNode("Last Name"));
  75. if(!dojo.isIE){
  76. tr.appendChild(th);
  77. }
  78. th = (dojo.isIE) ? tr.insertCell() : document.createElement("th");
  79. th.appendChild(document.createTextNode("First Name"));
  80. if(!dojo.isIE){
  81. tr.appendChild(th);
  82. }
  83. th = (dojo.isIE) ? tr.insertCell() : document.createElement("th");
  84. th.appendChild(document.createTextNode("Social Security"));
  85. if(!dojo.isIE){
  86. tr.appendChild(th);
  87. table.appendChild(tr);
  88. }
  89. for(var i = 1; i <= NUM_ROWS; i++){
  90. tr = (dojo.isIE) ? table.insertRow() : document.createElement("tr");
  91. tr.className = "data-item";
  92. var elem = (dojo.isIE) ? tr.insertCell() : document.createElement("td");
  93. elem.className = "last-name";
  94. var lastName = "Doe" + i;
  95. elem.appendChild(document.createTextNode(lastName));
  96. if(!dojo.isIE){
  97. tr.appendChild(elem);
  98. }
  99. elem = (dojo.isIE) ? tr.insertCell() : document.createElement("td");
  100. elem.className = "first-name";
  101. var firstName = "John" + i;
  102. elem.appendChild(document.createTextNode(firstName));
  103. if(!dojo.isIE){
  104. tr.appendChild(elem);
  105. }
  106. elem = elem = (dojo.isIE) ? tr.insertCell() : document.createElement("td");
  107. elem.className = "social-security";
  108. var ss = 513121500 + i;
  109. ss = new String(ss);
  110. ss = ss.slice(0, 3) + "-" + ss.slice(3, 5) + "-" + ss.slice(5);
  111. elem.appendChild(document.createTextNode(ss));
  112. if(!dojo.isIE){
  113. tr.appendChild(elem);
  114. table.appendChild(tr);
  115. }
  116. }
  117. document.body.appendChild(table);
  118. // reset button state
  119. dojo.byId("encrypt").disabled = false;
  120. dojo.byId("decrypt").disabled = true;
  121. }
  122. function readTable(){
  123. var data = [];
  124. var rows = dojo.query(".data-item");
  125. dojo.forEach(rows, function(row){
  126. var td = row.getElementsByTagName("td");
  127. var lastName = td[0].childNodes[0].nodeValue;
  128. var firstName = td[1].childNodes[0].nodeValue;
  129. var ssNumber = td[2].childNodes[0].nodeValue;
  130. data.push({lastName: lastName, firstName: firstName, ssNumber: ssNumber,
  131. toString: function(){
  132. return "{lastName: " + lastName
  133. + ", firstName: " + firstName
  134. + ", ssNumber: " + ssNumber
  135. + "}";
  136. }});
  137. });
  138. return data;
  139. }
  140. function setData(data){
  141. var rows = document.getElementsByTagName("tr");
  142. for(var i = 1; i < rows.length; i++){
  143. var customer = data[i - 1];
  144. var td = rows[i].getElementsByTagName("td");
  145. td[2].childNodes[0].nodeValue = customer.social_security;
  146. }
  147. }
  148. function encrypt(){
  149. // disable our buttons
  150. dojo.byId("encrypt").disabled = true;
  151. dojo.byId("decrypt").disabled = true;
  152. var data = readTable();
  153. var password = document.getElementById("password").value;
  154. // delete any old data
  155. dojox.sql("DELETE FROM CUSTOMERS");
  156. // insert new data
  157. insertCustomers(data, 0, password);
  158. }
  159. function insertCustomers(data, i, password){
  160. var nextIndex = i + 1;
  161. if(i >= data.length){
  162. var savedRows = dojox.sql("SELECT * FROM CUSTOMERS");
  163. setData(savedRows);
  164. return;
  165. }
  166. dojox.sql("INSERT INTO CUSTOMERS VALUES (?, ?, ENCRYPT(?))",
  167. data[i].lastName, data[i].firstName,
  168. data[i].ssNumber,
  169. password,
  170. function(results, error, errorMsg){
  171. // enable our buttons
  172. dojo.byId("encrypt").disabled = true;
  173. dojo.byId("decrypt").disabled = false;
  174. if(error == true){
  175. alert(errorMsg);
  176. return;
  177. }
  178. insertCustomers(data, nextIndex, password);
  179. }
  180. );
  181. }
  182. function decrypt(){
  183. // disable our buttons
  184. dojo.byId("encrypt").disabled = true;
  185. dojo.byId("decrypt").disabled = true;
  186. var password = document.getElementById("password").value;
  187. dojox.sql("SELECT last_name, first_name, DECRYPT(social_security) FROM CUSTOMERS",
  188. password,
  189. function(results, error, errorMsg){
  190. // enable our buttons
  191. dojo.byId("encrypt").disabled = false;
  192. dojo.byId("decrypt").disabled = true;
  193. if(error == true){
  194. alert(errorMsg);
  195. return;
  196. }
  197. setData(results);
  198. }
  199. );
  200. }
  201. </script>
  202. </head>
  203. <body>
  204. <h1>Dojo SQL Cryptography</h1>
  205. <h2>Instructions</h2>
  206. <p>This demo shows Dojo Offline's SQL encryption technologies. In the table below, we have a
  207. sample SQL table that has three columns of data: a last name, a first name, and
  208. a social security number. We don't want to store the social security numbers
  209. in the clear, just in case they are downloaded for offline use to a laptop and the
  210. laptop is stolen.</p>
  211. <p>To use this demo, enter a password and press the ENCRYPT button to see the Social Security column encrypt. Enter
  212. the same password and press DECRYPT to see it decrypt. If you enter an incorrect password and
  213. press DECRYPT, the Social Security column will remain encrypted and only show gibberish.</p>
  214. <p>Under the covers we use 256-bit AES encryption and your password to derive the crypto key; we use
  215. a facility in Google Gears to do the cryptography in such a way that the browser does not lock up
  216. during processing. Dojo Offline ties this cryptography into Dojo SQL, providing convenient ENCRYPT()
  217. and DECRYPT() SQL keywords you can use to easily have this functionality in your
  218. own offline applications. To learn how you can use this feature
  219. <a href="http://docs.google.com/View?docid=dhkhksk4_8gdp9gr#crypto" target="_blank">see here</a>.</p>
  220. <div id="cryptoContainer">
  221. <label for="password">
  222. Password:
  223. </label>
  224. <input type="input" name="password" id="password" value="sample_password">
  225. <button id="encrypt" onclick="window.setTimeout(encrypt, 1)">Encrypt</button>
  226. <button id="decrypt" onclick="window.setTimeout(decrypt, 1)" disabled="true">Decrypt</button>
  227. </div>
  228. <div id="numRowsContainer">
  229. <label for="numRows">
  230. Number of Customer Rows in Table:
  231. </label>
  232. <input id="numRows" type="input" value="30">
  233. <button onclick="createTable()">Update</button>
  234. </div>
  235. </body>
  236. </html>