@fireproof/core 0.7.0 → 0.7.2-dev

Sign up to get free protection for your applications and to get access to all the features.
package/dist/crypto.js CHANGED
@@ -1,5 +1,5 @@
1
1
  // @ts-nocheck
2
- import * as codec from 'encrypted-block';
2
+ import * as codec from './encrypted-block.js';
3
3
  import { create, load } from 'prolly-trees/cid-set';
4
4
  import { CID } from 'multiformats';
5
5
  import { encode, decode, create as mfCreate } from 'multiformats/block';
@@ -0,0 +1,48 @@
1
+ // from https://github.com/mikeal/encrypted-block
2
+ import randomBytes from 'randombytes';
3
+ import aes from 'js-crypto-aes';
4
+ import { CID } from 'multiformats';
5
+ const enc32 = value => {
6
+ value = +value;
7
+ const buff = new Uint8Array(4);
8
+ buff[3] = (value >>> 24);
9
+ buff[2] = (value >>> 16);
10
+ buff[1] = (value >>> 8);
11
+ buff[0] = (value & 0xff);
12
+ return buff;
13
+ };
14
+ const readUInt32LE = (buffer) => {
15
+ const offset = buffer.byteLength - 4;
16
+ return ((buffer[offset]) |
17
+ (buffer[offset + 1] << 8) |
18
+ (buffer[offset + 2] << 16)) +
19
+ (buffer[offset + 3] * 0x1000000);
20
+ };
21
+ const encode = ({ iv, bytes }) => concat([iv, bytes]);
22
+ const decode = bytes => {
23
+ const iv = bytes.subarray(0, 12);
24
+ bytes = bytes.slice(12);
25
+ return { iv, bytes };
26
+ };
27
+ const code = 0x300000 + 1337;
28
+ const concat = buffers => Uint8Array.from(buffers.map(b => [...b]).flat());
29
+ const decrypt = async ({ key, value }) => {
30
+ let { bytes, iv } = value;
31
+ bytes = await aes.decrypt(bytes, key, { name: 'AES-GCM', iv, tagLength: 16 });
32
+ const len = readUInt32LE(bytes.subarray(0, 4));
33
+ const cid = CID.decode(bytes.subarray(4, 4 + len));
34
+ bytes = bytes.subarray(4 + len);
35
+ return { cid, bytes };
36
+ };
37
+ const encrypt = async ({ key, cid, bytes }) => {
38
+ const len = enc32(cid.bytes.byteLength);
39
+ const iv = randomBytes(12);
40
+ const msg = concat([len, cid.bytes, bytes]);
41
+ bytes = await aes.encrypt(msg, key, { name: 'AES-GCM', iv, tagLength: 16 });
42
+ return { value: { bytes, iv } };
43
+ };
44
+ const crypto = key => {
45
+ return { encrypt: opts => encrypt({ key, ...opts }), decrypt: opts => decrypt({ key, ...opts }) };
46
+ };
47
+ const name = 'mikeal@encrypted-block:aes-gcm';
48
+ export { encode, decode, code, name, encrypt, decrypt, crypto };
@@ -38649,6 +38649,8 @@ aes$2.unwrapKey = unwrapKey;
38649
38649
 
38650
38650
  var aes = /*@__PURE__*/getDefaultExportFromCjs(dist$1);
38651
38651
 
38652
+ // from https://github.com/mikeal/encrypted-block
38653
+
38652
38654
  const enc32 = value => {
38653
38655
  value = +value;
38654
38656
  const buff = new Uint8Array(4);
@@ -38939,6 +38941,7 @@ class Base {
38939
38941
  const got = await reader.get(cid);
38940
38942
  let useCodec = codec;
38941
38943
  if (cid.toString().indexOf('bafy') === 0) {
38944
+ // @ts-ignore
38942
38945
  useCodec = codec$1; // todo this is a dirty check
38943
38946
  }
38944
38947
  const decoded = await decode$5({