crypto.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. var CryptoUtils = {
  2. cypher: "AES-GCM",
  3. key: null,
  4. urlSafeKey: null,
  5. initialize: function(key = null) {
  6. if(key === null) {
  7. return this.generateKey();
  8. }
  9. else {
  10. this.urlSafeKey = key;
  11. return this.importKey(this.base64ToArrayBuffer(key));
  12. }
  13. },
  14. generateKey: function() {
  15. var self = this;
  16. return window.crypto.subtle.generateKey(
  17. {
  18. name: this.cypher,
  19. length: 256,
  20. },
  21. true,
  22. ["encrypt", "decrypt"]
  23. ).then(function(k){
  24. self.key = k;
  25. return window.crypto.subtle.exportKey("raw", k)
  26. }).then(function(keydata) {
  27. console.log(keydata);
  28. self.urlSafeKey = self.arrayBufferToBase64(keydata);
  29. }).catch(function(err){
  30. console.error(err)
  31. });
  32. },
  33. encrypt: function(data) {
  34. var self = this;
  35. var iv = window.crypto.getRandomValues(new Uint8Array(12));
  36. return window.crypto.subtle.encrypt(
  37. {
  38. name: this.cypher,
  39. iv: iv,
  40. },
  41. this.key,
  42. data
  43. ).then(function(encrypted){
  44. return new Blob([iv, encrypted], {type: 'application/octet-binary'});
  45. });
  46. },
  47. encryptFilename: function(data, iv) {
  48. var self = this;
  49. iv = this.stringToArrayBuffer(iv);
  50. data = this.stringToArrayBuffer(data);
  51. return window.crypto.subtle.encrypt(
  52. {
  53. name: this.cypher,
  54. iv: iv,
  55. },
  56. this.key,
  57. data
  58. ).then(function(encrypted){
  59. return base32.encode(encrypted);
  60. });
  61. },
  62. decrypt: function(data) {
  63. var self = this;
  64. return window.crypto.subtle.decrypt(
  65. {
  66. name: this.cypher,
  67. iv: data.slice(0,12),
  68. },
  69. this.key,
  70. data.slice(12)
  71. );
  72. },
  73. decryptFileName: function(data, iv) {
  74. var self = this;
  75. var data = new Uint8Array(base32.decode.asBytes(data)).buffer;
  76. iv = this.stringToArrayBuffer(iv);
  77. return window.crypto.subtle.decrypt(
  78. {
  79. name: this.cypher,
  80. iv: iv,
  81. },
  82. this.key,
  83. data
  84. );
  85. },
  86. importKey: function(key) {
  87. var self = this;
  88. return window.crypto.subtle.importKey(
  89. "raw",
  90. key,
  91. {
  92. name: self.cypher
  93. },
  94. true,
  95. ["encrypt", "decrypt"]
  96. ).then(function(k){
  97. self.key = k;
  98. });
  99. },
  100. arrayBufferToBase64: function(a) {
  101. return btoa(String.fromCharCode(...new Uint8Array(a)))
  102. },
  103. base64ToArrayBuffer: function(b) {
  104. var str = atob(b);
  105. var buf = new ArrayBuffer(str.length);
  106. var bufView = new Uint8Array(buf);
  107. for (var i=0, strLen=str.length; i<strLen; i++) {
  108. bufView[i] = str.charCodeAt(i);
  109. }
  110. return buf;
  111. },
  112. stringToArrayBuffer: function(str) {
  113. var utf8 = str;
  114. return new Uint8Array(utf8.split('').map(function (item) {
  115. return item.charCodeAt();
  116. })).buffer;
  117. },
  118. arrayBufferToString: function(buf) {
  119. buf = new Uint8Array(buf);
  120. var utf8 = Array.from(buf).map(function (item) {
  121. return String.fromCharCode(item);
  122. }).join('');
  123. return utf8;
  124. }
  125. }