-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: type check crypto functions (#1144)
- Loading branch information
1 parent
51d13e3
commit c0085eb
Showing
10 changed files
with
111 additions
and
36 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
--- | ||
--- |
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,15 @@ | ||
import type { CryptoApi } from '../types'; | ||
|
||
import { bufferFromString, stringFromBuffer, decrypt, encrypt, keyFromPassword } from './aes-ctr'; | ||
import { randomBytes } from './randomBytes'; | ||
|
||
const api: CryptoApi = { | ||
bufferFromString, | ||
stringFromBuffer, | ||
decrypt, | ||
encrypt, | ||
keyFromPassword, | ||
randomBytes, | ||
}; | ||
|
||
export default api; |
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 |
---|---|---|
@@ -1,6 +1,8 @@ | ||
import type { CryptoApi } from '../types'; | ||
|
||
import { crypto } from './crypto'; | ||
|
||
export const randomBytes = (length: number) => { | ||
export const randomBytes: CryptoApi['randomBytes'] = (length: number): Uint8Array => { | ||
const randomValues = crypto.getRandomValues(new Uint8Array(length)); | ||
return randomValues; | ||
}; |
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 |
---|---|---|
@@ -1,3 +1,12 @@ | ||
export * from './browser/aes-ctr'; | ||
export * from './browser/randomBytes'; | ||
import cryptoApi from './browser'; | ||
|
||
export * from './types'; | ||
|
||
export const { | ||
bufferFromString, | ||
decrypt, | ||
encrypt, | ||
keyFromPassword, | ||
randomBytes, | ||
stringFromBuffer, | ||
} = cryptoApi; |
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 |
---|---|---|
@@ -1,3 +1,12 @@ | ||
export * from './node/aes-ctr'; | ||
export * from './node/randomBytes'; | ||
import cryptoApi from './node'; | ||
|
||
export * from './types'; | ||
|
||
export const { | ||
bufferFromString, | ||
decrypt, | ||
encrypt, | ||
keyFromPassword, | ||
randomBytes, | ||
stringFromBuffer, | ||
} = cryptoApi; |
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,15 @@ | ||
import type { CryptoApi } from '../types'; | ||
|
||
import { bufferFromString, stringFromBuffer, decrypt, encrypt, keyFromPassword } from './aes-ctr'; | ||
import { randomBytes } from './randomBytes'; | ||
|
||
const api: CryptoApi = { | ||
bufferFromString, | ||
stringFromBuffer, | ||
decrypt, | ||
encrypt, | ||
keyFromPassword, | ||
randomBytes, | ||
}; | ||
|
||
export default api; |
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 |
---|---|---|
@@ -1,6 +1,8 @@ | ||
import crypto from 'crypto'; | ||
|
||
export const randomBytes = (length: number) => { | ||
const randomValues = crypto.randomBytes(length); | ||
import type { CryptoApi } from '../types'; | ||
|
||
export const randomBytes: CryptoApi['randomBytes'] = (length: number): Uint8Array => { | ||
const randomValues = Uint8Array.from(crypto.randomBytes(length)); | ||
return randomValues; | ||
}; |
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