a collection of lightweight TypeScript packages for AT Protocol, the protocol powering Bluesky
atproto bluesky typescript npm
101
fork

Configure Feed

Select the types of activity you want to include in your feed.

feat(uint8array): add randomBytes function

Mary 27720331 dd73ee7b

+33 -1
+5
.changeset/nice-owls-mate.md
··· 1 + --- 2 + '@atcute/uint8array': minor 3 + --- 4 + 5 + add randomBytes function
+9
packages/misc/uint8array/lib/index.bun.ts
··· 158 158 export const toSha256 = async (buffer: Uint8Array): Promise<Uint8Array<ArrayBuffer>> => { 159 159 return toUint8Array(_hash('sha256', buffer, 'buffer')) as Uint8Array<ArrayBuffer>; 160 160 }; 161 + 162 + /** 163 + * generates cryptographically secure random bytes 164 + * @param size number of bytes to generate 165 + * @returns buffer filled with random bytes 166 + */ 167 + export const randomBytes = (size: number): Uint8Array<ArrayBuffer> => { 168 + return crypto.getRandomValues(new Uint8Array(size)); 169 + };
+10 -1
packages/misc/uint8array/lib/index.node.ts
··· 1 1 import { Buffer as NodeBuffer } from 'node:buffer'; 2 - import { hash as _hash, timingSafeEqual as _timingSafeEqual } from 'node:crypto'; 2 + import { hash as _hash, randomFillSync as _randomFillSync, timingSafeEqual as _timingSafeEqual } from 'node:crypto'; 3 3 4 4 const _alloc = /*#__PURE__*/ NodeBuffer.alloc; 5 5 const _allocUnsafe = /*#__PURE__*/ NodeBuffer.allocUnsafe; ··· 151 151 export const toSha256 = async (buffer: Uint8Array): Promise<Uint8Array<ArrayBuffer>> => { 152 152 return toUint8Array(_hash('sha256', buffer, 'buffer')) as Uint8Array<ArrayBuffer>; 153 153 }; 154 + 155 + /** 156 + * generates cryptographically secure random bytes 157 + * @param size number of bytes to generate 158 + * @returns buffer filled with random bytes 159 + */ 160 + export const randomBytes = (size: number): Uint8Array<ArrayBuffer> => { 161 + return _randomFillSync(toUint8Array(_allocUnsafe(size))) as Uint8Array<ArrayBuffer>; 162 + };
+9
packages/misc/uint8array/lib/index.ts
··· 279 279 export const toSha256 = async (buffer: Uint8Array<ArrayBuffer>): Promise<Uint8Array<ArrayBuffer>> => { 280 280 return new Uint8Array(await subtle.digest('SHA-256', buffer)); 281 281 }; 282 + 283 + /** 284 + * generates cryptographically secure random bytes 285 + * @param size number of bytes to generate 286 + * @returns buffer filled with random bytes 287 + */ 288 + export const randomBytes = (size: number): Uint8Array<ArrayBuffer> => { 289 + return crypto.getRandomValues(new Uint8Array(size)); 290 + };