-
-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
62 changed files
with
2,046 additions
and
165 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { _decrypt, _encrypt, ENCRYPTION_KEY_BYTES, getKeyDigest, loadKey } from './utils'; | ||
|
||
/** | ||
* Default encrypter | ||
*/ | ||
export class Encrypter { | ||
private key: CryptoKey | undefined; | ||
private keyDigest: string | undefined; | ||
|
||
constructor(private readonly encryptionKey: Uint8Array) { | ||
if (encryptionKey.length !== ENCRYPTION_KEY_BYTES) { | ||
throw new Error(`Encryption key must be ${ENCRYPTION_KEY_BYTES} bytes`); | ||
} | ||
} | ||
|
||
/** | ||
* Encrypts the given data | ||
*/ | ||
async encrypt(data: string): Promise<string> { | ||
if (!this.key) { | ||
this.key = await loadKey(this.encryptionKey, ['encrypt']); | ||
} | ||
|
||
if (!this.keyDigest) { | ||
this.keyDigest = await getKeyDigest(this.encryptionKey); | ||
} | ||
|
||
return _encrypt(data, this.key, this.keyDigest); | ||
} | ||
} | ||
|
||
/** | ||
* Default decrypter | ||
*/ | ||
export class Decrypter { | ||
private keys: Array<{ key: CryptoKey; digest: string }> = []; | ||
|
||
constructor(private readonly decryptionKeys: Uint8Array[]) { | ||
if (decryptionKeys.length === 0) { | ||
throw new Error('At least one decryption key must be provided'); | ||
} | ||
|
||
for (const key of decryptionKeys) { | ||
if (key.length !== ENCRYPTION_KEY_BYTES) { | ||
throw new Error(`Decryption key must be ${ENCRYPTION_KEY_BYTES} bytes`); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Decrypts the given data | ||
*/ | ||
async decrypt(data: string): Promise<string> { | ||
if (this.keys.length === 0) { | ||
this.keys = await Promise.all( | ||
this.decryptionKeys.map(async (key) => ({ | ||
key: await loadKey(key, ['decrypt']), | ||
digest: await getKeyDigest(key), | ||
})) | ||
); | ||
} | ||
|
||
return _decrypt(data, async (digest) => | ||
this.keys.filter((entry) => entry.digest === digest).map((entry) => entry.key) | ||
); | ||
} | ||
} |
Oops, something went wrong.