Skip to content

Commit

Permalink
Move utility functions to a separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
Shigoto-dev19 committed May 6, 2024
1 parent 4d89996 commit ad894ab
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 27 deletions.
31 changes: 4 additions & 27 deletions src/base64.test.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,9 @@
import { Bytes } from 'o1js';
import { base64Decode } from './base64';
import { randomBytes as generateRandomBytes } from 'node:crypto';

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;
}
import {
calculateB64DecodedBytesLength,
generateRandomBase64String,
} from './utils';

describe('Base64 Decode Tests', () => {
function testBase64Decode(base64String: string) {
Expand Down
29 changes: 29 additions & 0 deletions src/utils.ts
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;
}

0 comments on commit ad894ab

Please sign in to comment.