|
| 1 | +namespace $ { |
| 2 | + |
| 3 | + const algorithm = { |
| 4 | + name: 'AES-GCM', |
| 5 | + length: 128, |
| 6 | + tagLength: 32, |
| 7 | + } |
| 8 | + |
| 9 | + /** Symmetric cipher with shortest payload */ |
| 10 | + export class $piterjs_secret extends Object { |
| 11 | + |
| 12 | + /** Key size in bytes. */ |
| 13 | + static size = 16 |
| 14 | + |
| 15 | + constructor( |
| 16 | + readonly native: CryptoKey & { type: 'secret' } |
| 17 | + ) { |
| 18 | + super() |
| 19 | + } |
| 20 | + |
| 21 | + static async generate() { |
| 22 | + return new this( |
| 23 | + await $mol_crypto_native.subtle.generateKey( |
| 24 | + algorithm, |
| 25 | + true, |
| 26 | + [ 'encrypt', 'decrypt' ] |
| 27 | + ) as CryptoKey & { type: 'secret' } |
| 28 | + ) |
| 29 | + } |
| 30 | + |
| 31 | + static async from( serial: BufferSource | string ) { |
| 32 | + |
| 33 | + if( typeof serial === 'string' ) { |
| 34 | + serial = $mol_charset_encode( serial ) |
| 35 | + serial = await $mol_crypto_native.subtle.digest( 'SHA-256', serial ) |
| 36 | + } |
| 37 | + |
| 38 | + return new this( |
| 39 | + await $mol_crypto_native.subtle.importKey( |
| 40 | + 'raw', |
| 41 | + serial, |
| 42 | + algorithm, |
| 43 | + true, |
| 44 | + [ 'encrypt', 'decrypt' ], |
| 45 | + ) as CryptoKey & { type: 'secret' } |
| 46 | + ) |
| 47 | + |
| 48 | + } |
| 49 | + |
| 50 | + static async derive( private_serial: string, public_serial: string ) { |
| 51 | + |
| 52 | + const ecdh = { name: "ECDH", namedCurve: "P-256" } |
| 53 | + const jwk = { crv: 'P-256', ext: true, kty: 'EC' } |
| 54 | + |
| 55 | + const private_key = await $mol_crypto_native.subtle.importKey( |
| 56 | + 'jwk', |
| 57 | + { |
| 58 | + ... jwk, |
| 59 | + key_ops: [ 'deriveKey' ], |
| 60 | + x: private_serial.slice( 0, 43 ), |
| 61 | + y: private_serial.slice( 43, 86 ), |
| 62 | + d: private_serial.slice( 86, 129 ), |
| 63 | + }, |
| 64 | + ecdh, |
| 65 | + true, |
| 66 | + [ 'deriveKey' ], |
| 67 | + ) |
| 68 | + |
| 69 | + const public_key = await $mol_crypto_native.subtle.importKey( |
| 70 | + 'jwk', |
| 71 | + { |
| 72 | + ... jwk, |
| 73 | + key_ops: [], |
| 74 | + x: public_serial.slice( 0, 43 ), |
| 75 | + y: public_serial.slice( 43, 86 ), |
| 76 | + }, |
| 77 | + ecdh, |
| 78 | + true, |
| 79 | + [], |
| 80 | + ) |
| 81 | + |
| 82 | + const secret = await $mol_crypto_native.subtle.deriveKey( |
| 83 | + { |
| 84 | + name: "ECDH", |
| 85 | + public: public_key, |
| 86 | + }, |
| 87 | + private_key, |
| 88 | + algorithm, |
| 89 | + true, |
| 90 | + [ "encrypt", "decrypt" ], |
| 91 | + ) |
| 92 | + |
| 93 | + return new this( secret as CryptoKey & { type: 'secret' } ) |
| 94 | + } |
| 95 | + |
| 96 | + /** 16 bytes */ |
| 97 | + async serial() { |
| 98 | + return await $mol_crypto_native.subtle.exportKey( |
| 99 | + 'raw', |
| 100 | + this.native, |
| 101 | + ) |
| 102 | + } |
| 103 | + |
| 104 | + /** 16n bytes */ |
| 105 | + async encrypt( open: BufferSource, salt: BufferSource ): Promise< ArrayBuffer > { |
| 106 | + return await $mol_crypto_native.subtle.encrypt( |
| 107 | + { |
| 108 | + ... algorithm, |
| 109 | + iv: salt, |
| 110 | + }, |
| 111 | + this.native, |
| 112 | + open |
| 113 | + ) |
| 114 | + } |
| 115 | + |
| 116 | + async decrypt( closed: BufferSource, salt : BufferSource ): Promise< ArrayBuffer > { |
| 117 | + return await $mol_crypto_native.subtle.decrypt( |
| 118 | + { |
| 119 | + ... algorithm, |
| 120 | + iv: salt, |
| 121 | + }, |
| 122 | + this.native, |
| 123 | + closed |
| 124 | + ) |
| 125 | + } |
| 126 | + |
| 127 | + } |
| 128 | + |
| 129 | +} |
0 commit comments