-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move utility functions to a separate file
- Loading branch information
1 parent
4d89996
commit ad894ab
Showing
2 changed files
with
33 additions
and
27 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,29 @@ | ||
import { randomBytes as generateRandomBytes } from 'node:crypto'; | ||
|
||
export { generateRandomBase64String, calculateB64DecodedBytesLength }; | ||
|
||
function generateRandomBase64String(maxLength: number): string { | ||
// Generate a random length between 1 and maxLength | ||
const length = Math.floor(Math.random() * maxLength) + 1; | ||
|
||
// Generate random bytes or buffer | ||
const randomBytes = generateRandomBytes(length); | ||
|
||
// Convert to Base64 | ||
const base64String = randomBytes.toString('base64'); | ||
|
||
return base64String; | ||
} | ||
|
||
function calculateB64DecodedBytesLength(base64String: string): number { | ||
// Calculate the length of the base64-encoded string | ||
const base64Length = base64String.length; | ||
|
||
// Count the number of padding characters '=' in the base64 string | ||
const padding = (base64String.match(/=/g) || []).length; | ||
|
||
// Calculate the length of the decoded bytes | ||
const byteLength = (base64Length * 3) / 4 - padding; | ||
|
||
return byteLength; | ||
} |