@oslojs/crypto
Advanced tools
Comparing version 0.5.3 to 0.6.0
@@ -1,2 +0,1 @@ | ||
import { xor } from "@oslojs/binary"; | ||
export class HMAC { | ||
@@ -22,7 +21,11 @@ k0; | ||
const ipad = new Uint8Array(this.k0.byteLength).fill(0x36); | ||
xor(ipad, this.k0); | ||
for (let i = 0; i < ipad.byteLength; i++) { | ||
ipad[i] ^= this.k0[i]; | ||
} | ||
this.inner.update(ipad); | ||
this.outer = new Algorithm(); | ||
const opad = new Uint8Array(this.k0.byteLength).fill(0x5c); | ||
xor(opad, this.k0); | ||
for (let i = 0; i < opad.byteLength; i++) { | ||
opad[i] ^= this.k0[i]; | ||
} | ||
this.outer.update(opad); | ||
@@ -29,0 +32,0 @@ } |
@@ -1,6 +0,6 @@ | ||
export declare function random(): number; | ||
export declare function generateRandomInteger(max: bigint): bigint; | ||
export declare function generateRandomIntegerNumber(max: number): number; | ||
export declare function generateRandomString(length: number, alphabet: string): string; | ||
export type AlphabetPattern = "a-z" | "A-Z" | "0-9" | "-" | "_"; | ||
export declare function alphabet(...patterns: AlphabetPattern[]): string; | ||
export declare function generateRandomInteger(random: RandomReader, max: bigint): bigint; | ||
export declare function generateRandomIntegerNumber(random: RandomReader, max: number): number; | ||
export declare function generateRandomString(random: RandomReader, alphabet: string, length: number): string; | ||
export interface RandomReader { | ||
read(bytes: Uint8Array): void; | ||
} |
import { bigIntFromBytes } from "@oslojs/binary"; | ||
export function random() { | ||
const buffer = new ArrayBuffer(8); | ||
const bytes = crypto.getRandomValues(new Uint8Array(buffer)); | ||
// sets the exponent value (11 bits) to 01111111111 (1023) | ||
// since the bias is 1023 (2 * (11 - 1) - 1), 1023 - 1023 = 0 | ||
// 2^0 * (1 + [52 bit number between 0-1]) = number between 1-2 | ||
bytes[0] = 63; | ||
bytes[1] = bytes[1] | 240; | ||
return new DataView(buffer).getFloat64(0) - 1; | ||
} | ||
export function generateRandomInteger(max) { | ||
export function generateRandomInteger(random, max) { | ||
if (max < 2) { | ||
@@ -19,3 +9,10 @@ throw new Error("Argument 'max' must be a positive integer larger than 1"); | ||
const bytes = new Uint8Array(Math.ceil(inclusiveMaxBitLength / 8)); | ||
crypto.getRandomValues(bytes); | ||
try { | ||
random.read(bytes); | ||
} | ||
catch (e) { | ||
throw new Error("Failed to retrieve random bytes", { | ||
cause: e | ||
}); | ||
} | ||
// This zeroes bits that can be ignored to increase the chance `result` < `max`. | ||
@@ -28,3 +25,10 @@ // For example, if `max` can be represented with 10 bits, the leading 6 bits of the random 16 bits (2 bytes) can be ignored. | ||
while (result >= max) { | ||
crypto.getRandomValues(bytes); | ||
try { | ||
random.read(bytes); | ||
} | ||
catch (e) { | ||
throw new Error("Failed to retrieve random bytes", { | ||
cause: e | ||
}); | ||
} | ||
if (shift !== 0) { | ||
@@ -37,33 +41,14 @@ bytes[0] &= (1 << shift) - 1; | ||
} | ||
export function generateRandomIntegerNumber(max) { | ||
export function generateRandomIntegerNumber(random, max) { | ||
if (max < 2 || max > Number.MAX_SAFE_INTEGER) { | ||
throw new Error("Argument 'max' must be a positive integer larger than 1"); | ||
} | ||
return Number(generateRandomInteger(BigInt(max))); | ||
return Number(generateRandomInteger(random, BigInt(max))); | ||
} | ||
export function generateRandomString(length, alphabet) { | ||
export function generateRandomString(random, alphabet, length) { | ||
let result = ""; | ||
for (let i = 0; i < length; i++) { | ||
result += alphabet[generateRandomIntegerNumber(alphabet.length)]; | ||
result += alphabet[generateRandomIntegerNumber(random, alphabet.length)]; | ||
} | ||
return result; | ||
} | ||
export function alphabet(...patterns) { | ||
const patternSet = new Set(patterns); | ||
let result = ""; | ||
for (const pattern of patternSet) { | ||
if (pattern === "a-z") { | ||
result += "abcdefghijklmnopqrstuvwxyz"; | ||
} | ||
else if (pattern === "A-Z") { | ||
result += "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; | ||
} | ||
else if (pattern === "0-9") { | ||
result += "0123456789"; | ||
} | ||
else { | ||
result += pattern; | ||
} | ||
} | ||
return result; | ||
} |
@@ -1,2 +0,2 @@ | ||
import { bigIntFromBytes, concatenateBytes, DynamicBuffer, xor } from "@oslojs/binary"; | ||
import { bigIntFromBytes, concatenateBytes, DynamicBuffer } from "@oslojs/binary"; | ||
import { constantTimeEqual } from "../subtle/index.js"; | ||
@@ -53,3 +53,5 @@ import { ASN1BitString, ASN1EncodableSequence, ASN1Integer, ASN1Null, ASN1ObjectIdentifier, ASN1OctetString, ASN1UniversalType, encodeASN1, encodeObjectIdentifier, parseASN1NoLeftoverBytes } from "@oslojs/asn1"; | ||
const dbMask = mgf1(MGF1HashAlgorithm, h, em.byteLength - hashed.byteLength - 1); | ||
xor(db, dbMask); | ||
for (let i = 0; i < db.byteLength; i++) { | ||
db[i] ^= dbMask[i]; | ||
} | ||
for (let i = 0; i < Math.floor((em.byteLength - hashed.byteLength - 1) / 8); i++) { | ||
@@ -142,3 +144,4 @@ db[i] = 0; | ||
const zcHash = new Hash(); | ||
zcHash.update(concatenateBytes(Z, counterBytes)); | ||
zcHash.update(Z); | ||
zcHash.update(counterBytes); | ||
t = concatenateBytes(t, zcHash.digest()); | ||
@@ -145,0 +148,0 @@ counter++; |
{ | ||
"name": "@oslojs/crypto", | ||
"type": "module", | ||
"version": "0.5.3", | ||
"version": "0.6.0", | ||
"description": "A very basic crypto library", | ||
@@ -6,0 +6,0 @@ "files": [ |
@@ -18,17 +18,1 @@ # @oslojs/crypto documentation | ||
``` | ||
## Prerequisites | ||
This package requires the [Web Crypto API](https://developer.mozilla.org/en-US/docs/Web/API/Web_Crypto_API). This is available in most modern runtimes, including Node.js 20+, Deno, Bun, and Cloudflare Workers. The big exception is Node.js 16 and 18. Make sure to polyfill it using `webcrypto`. | ||
```ts | ||
import { webcrypto } from "node:crypto"; | ||
globalThis.crypto = webcrypto; | ||
``` | ||
Alternatively, add the `--experimental-global-webcrypto` flag when executing files. | ||
``` | ||
node --experimental-global-webcrypto index.js | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
73354
1931
18