add generate key

This commit is contained in:
2023-01-13 20:42:36 +01:00
parent f2f8607c39
commit d1b07e0a81
6 changed files with 244 additions and 129 deletions

View File

@@ -29,7 +29,8 @@ export enum Modes {
export class AES {
static Modes = Modes
private static delimiter = '--' // delimiter with a character that is not allowed in base64 or hex
// delimiter with a character that is not allowed in base64 or hex
private static delimiter = '--'
private static delimiterEasy = '---'
private static InvalidCiphertext = new Error('Invalid ciphertext')
@@ -141,4 +142,18 @@ export class AES {
const decrypted = await this.decrypt(data, keyDerived)
return Bytes.decode(decrypted)
}
static async generateKey(): Promise<TypedArray> {
const c = await getCrypto()
const key = await c.subtle.generateKey(
{
name: 'AES-GCM',
length: 256,
},
true,
['encrypt', 'decrypt']
)
const buffer = await c.subtle.exportKey('raw', key)
return new Uint8Array(buffer)
}
}

View File

@@ -3,4 +3,4 @@ export * from './crypto/encoding.js'
export * from './crypto/hash.js'
export * from './crypto/random.js'
export * from './crypto/rsa.js'
export * from './utils/base.js'
export { TypedArray } from './utils/base.js'