crypto-tests.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import assert from 'assert';
  2. import { arrayToB64, b64ToArray } from '../../../app/utils';
  3. describe('webcrypto', function() {
  4. it('can do it', async function() {
  5. const encoder = new TextEncoder();
  6. const x = b64ToArray('SPIfAlwbnncIFw3hEHYihw');
  7. const a = await crypto.subtle.importKey('raw', x, 'PBKDF2', false, [
  8. 'deriveKey'
  9. ]);
  10. const ad = await crypto.subtle.deriveKey(
  11. {
  12. name: 'PBKDF2',
  13. salt: encoder.encode('metadata'),
  14. iterations: 100,
  15. hash: 'SHA-256'
  16. },
  17. a,
  18. {
  19. name: 'AES-GCM',
  20. length: 128
  21. },
  22. false,
  23. ['encrypt', 'decrypt']
  24. );
  25. const ae = await crypto.subtle.encrypt(
  26. {
  27. name: 'AES-GCM',
  28. iv: new Uint8Array(12),
  29. tagLength: 128
  30. },
  31. ad,
  32. encoder.encode('hello world!')
  33. );
  34. assert.equal(
  35. arrayToB64(new Uint8Array(ae)),
  36. 'UXQQ4yVf55TRk9AZtz5QCwFofRvh-HdWJyxSCQ'
  37. );
  38. const ah = await crypto.subtle.deriveKey(
  39. {
  40. name: 'PBKDF2',
  41. salt: encoder.encode('authentication'),
  42. iterations: 100,
  43. hash: 'SHA-256'
  44. },
  45. a,
  46. {
  47. name: 'HMAC',
  48. hash: { name: 'SHA-256' }
  49. },
  50. true,
  51. ['sign']
  52. );
  53. const ahx = await crypto.subtle.exportKey('raw', ah);
  54. assert.equal(
  55. arrayToB64(new Uint8Array(ahx)),
  56. 'wxXDmHgmMgrcDVD8zbDLRl2yNa8jSAQgsaeIBZ4vueygpxzaTK6ZE_6X-XHvllBly6pSuFNbSxcve0ZHhVdcEA'
  57. );
  58. // const jwk = await crypto.subtle.exportKey('jwk', ah)
  59. // console.error(jwk)
  60. const as = await crypto.subtle.sign(
  61. {
  62. name: 'HMAC'
  63. },
  64. ah,
  65. encoder.encode('test')
  66. );
  67. assert.equal(
  68. arrayToB64(new Uint8Array(as)),
  69. 'AOi4HcoCJxQ4nUYxlmHB1rlcxQBn-zVjrSHz-VW7S-I'
  70. );
  71. const b = await crypto.subtle.importKey('raw', x, 'HKDF', false, [
  72. 'deriveKey'
  73. ]);
  74. const bd = await crypto.subtle.deriveKey(
  75. {
  76. name: 'HKDF',
  77. salt: new Uint8Array(),
  78. info: encoder.encode('encryption'),
  79. hash: 'SHA-256'
  80. },
  81. b,
  82. {
  83. name: 'AES-GCM',
  84. length: 128
  85. },
  86. true,
  87. ['encrypt', 'decrypt']
  88. );
  89. const bdx = await crypto.subtle.exportKey('raw', bd);
  90. assert.equal(arrayToB64(new Uint8Array(bdx)), 'g7okjWWO9yueDz16-owShQ');
  91. const bh = await crypto.subtle.deriveKey(
  92. {
  93. name: 'HKDF',
  94. salt: new Uint8Array(),
  95. info: encoder.encode('authentication'),
  96. hash: 'SHA-256'
  97. },
  98. b,
  99. {
  100. name: 'HMAC',
  101. hash: { name: 'SHA-256' }
  102. },
  103. true,
  104. ['sign']
  105. );
  106. const bhx = await crypto.subtle.exportKey('raw', bh);
  107. assert.equal(
  108. arrayToB64(new Uint8Array(bhx)),
  109. 'TQOGtmQ8-ZfnWu6Iq-U1IAVBVREFuI17xqsW1shiC8eMCa-a5qeYTvoX3-5kCoCha8R59ycnPDnTz75clLBmbQ'
  110. );
  111. });
  112. });