-
Notifications
You must be signed in to change notification settings - Fork 1
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
1 parent
b570dea
commit efc05dd
Showing
5 changed files
with
82 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { concatUnit8 } from '@/utilities/arr'; | ||
import { uint8ToHex } from 'bigint-toolkit'; | ||
import sodium from 'libsodium-wrappers'; | ||
|
||
export default new class EncryptionService { | ||
encrypt(str: string, key: string) { | ||
// const nonce = sodium.randombytes_buf(24); | ||
// const salt = sodium.randombytes_buf(32); | ||
|
||
const k = sodium.from_hex(key); | ||
|
||
const nonce = sodium.randombytes_buf(sodium.crypto_secretbox_NONCEBYTES); | ||
const salt = sodium.randombytes_buf(32); | ||
|
||
const saltStr = new TextDecoder().decode(salt); | ||
|
||
const encKey = sodium.crypto_generichash(32, saltStr + ':ENC', k); | ||
const hmacKey = sodium.crypto_generichash(32, saltStr + ':AUTH', k); | ||
|
||
const enc = sodium.crypto_secretbox_easy(str, nonce, encKey); | ||
|
||
const hmac = sodium.crypto_generichash( | ||
64, | ||
concatUnit8(salt, nonce, enc), | ||
hmacKey | ||
); | ||
|
||
return uint8ToHex(concatUnit8(salt, nonce, enc, hmac)); | ||
} | ||
} |
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,15 @@ | ||
|
||
export function concatUnit8(...items: Uint8Array[]) { | ||
const length = items.reduce( | ||
(len, item) => len + item.length, | ||
0 | ||
); | ||
|
||
const result = new Uint8Array(length); | ||
|
||
for (const item of items) { | ||
result.set(item); | ||
} | ||
|
||
return result; | ||
} |
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