-
-
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.
♻️ refactor(tests): fix testing, fix some bugs in the new pkgs
Signed-off-by: Pauline <[email protected]>
- Loading branch information
1 parent
e458fc7
commit 551b1c9
Showing
191 changed files
with
3,533 additions
and
3,163 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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
registry=https://registry.npmjs.org | ||
@jsr:registry=https://npm.jsr.io | ||
strict-dep-builds=true |
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 |
---|---|---|
|
@@ -48,5 +48,5 @@ | |
"postcss" | ||
], | ||
|
||
"npm.packageManager": "pnpm", | ||
"npm.packageManager": "pnpm" | ||
} |
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,19 +1,6 @@ | ||
import process from 'node:process'; | ||
import { defineConfig } from 'bumpp'; | ||
|
||
export default defineConfig({ | ||
all: false, | ||
commit: true, | ||
confirm: true, | ||
cwd: process.cwd(), | ||
ignoreScripts: false, | ||
interface: true, | ||
noGitCheck: true, | ||
noVerify: false, | ||
preid: 'beta', | ||
push: true, | ||
recursive: true, | ||
release: 'prompt', | ||
sign: true, | ||
tag: true, | ||
}); |
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
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,13 @@ | ||
# @flowr/crypto | ||
|
||
**A simple, type-safe cryptography utility library.** | ||
|
||
## Installation | ||
|
||
You can use the following command to install this package, or replace `pnpm add` with your package manager of choice. | ||
|
||
```sh | ||
pnpm add @flowr/crypto | ||
``` | ||
|
||
## Usage |
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 +1,75 @@ | ||
{} | ||
{ | ||
"$schema": "https://json.schemastore.org/package.json", | ||
"name": "@flowr/crypto", | ||
"type": "module", | ||
"version": "5.2.0", | ||
"description": "A simple, type-safe cryptography utility library.", | ||
"author": "@flowr", | ||
"contributors": ["Pauline <[email protected]>"], | ||
"license": "(LicenseRef-OQL-1.2 OR MIT OR Apache-2.0)", | ||
"funding": "https://ko-fi.com/pauliesnug", | ||
"homepage": "https://petal.dyn.gay", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/pulseflow/petal.git", | ||
"directory": "packages/result" | ||
}, | ||
"bugs": "https://github.com/pulseflow/petal/issues", | ||
"keywords": [ | ||
"@flowr/crypto", | ||
"petal", | ||
"flowr", | ||
"typescript", | ||
"ts", | ||
"standalone" | ||
], | ||
"sideEffects": false, | ||
"exports": { | ||
".": { | ||
"import": { | ||
"types": "./dist/esm/index.d.ts", | ||
"default": "./dist/esm/index.js" | ||
}, | ||
"require": { | ||
"types": "./dist/cjs/index.d.cts", | ||
"default": "./dist/cjs/index.cjs" | ||
}, | ||
"browser": "./dist/iife/index.global.js" | ||
}, | ||
"./package.json": "./package.json" | ||
}, | ||
"main": "./dist/cjs/index.cjs", | ||
"module": "./dist/esm/index.js", | ||
"browser": "./dist/iife/index.global.js", | ||
"unpkg": "./dist/iife/index.global.js", | ||
"types": "./dist/esm/index.d.ts", | ||
"files": [ | ||
"LICENSE*", | ||
"README.md", | ||
"dist/**/*.cjs", | ||
"dist/**/*.d.cts", | ||
"dist/**/*.d.mts", | ||
"dist/**/*.d.ts", | ||
"dist/**/*.js", | ||
"dist/**/*.mjs" | ||
], | ||
"engines": { | ||
"node": ">=22" | ||
}, | ||
"scripts": { | ||
"build": "pnpm gen && tsup && pnpm build:cjs", | ||
"start": "pnpm gen && tsup --watch", | ||
"build:cjs": "tsx ../scripts/src/cjs.ts", | ||
"gen": "tsx ../scripts/src/gen.ts crypto --write" | ||
}, | ||
"devDependencies": { | ||
"@std/crypto": "npm:@jsr/std__crypto@^1.0.3", | ||
"@std/encoding": "npm:@jsr/std__encoding@^1.0.3", | ||
"scule": "^1.3.0", | ||
"tweetnacl": "^1.0.3" | ||
}, | ||
"publishConfig": { | ||
"access": "public", | ||
"provenance": true | ||
} | ||
} |
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 @@ | ||
export * from './lib/advanced.ts'; | ||
export * from './lib/base.ts'; |
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,77 @@ | ||
import type { Base, Variation } from './base.ts'; | ||
import nacl from 'tweetnacl'; | ||
import b from './base.ts'; | ||
|
||
export type Bit = 0 | 1; | ||
export type BufferLike = Uint8Array | ArrayBuffer | string; | ||
export const rand: (n: number) => Uint8Array = n => nacl.randomBytes(n); | ||
export const x25519: (n: Uint8Array) => Uint8Array = n => nacl.scalarMult.base(n); | ||
export const sha512: (msg: BufferLike) => Uint8Array = msg => nacl.hash(toUint8Array(msg)); | ||
export const sha256: (msg: BufferLike) => Promise<Uint8Array> = async msg => crypto.subtle.digest('SHA-256', toUint8Array(msg)).then(toUint8Array); | ||
export function pair(priv?: Uint8Array): readonly [Uint8Array, Uint8Array] { | ||
priv = priv ?? rand(32); | ||
return [priv, x25519(priv)] as const; | ||
} | ||
|
||
export function toUint8Array(buf: BufferLike): Uint8Array; | ||
export function toUint8Array<B extends Base>(buf: string, base: B, variation?: Variation<B>): Uint8Array; | ||
export function toUint8Array<B extends Base | undefined>(buf: BufferLike, base?: B, variation?: Variation<NonNullable<B>>): Uint8Array { | ||
if (typeof buf === 'string') { | ||
if (base) | ||
return b(base, variation!).decode(buf); | ||
|
||
return new TextEncoder().encode(buf); | ||
} | ||
|
||
return new Uint8Array(buf); | ||
} | ||
|
||
export function verify(msg: Uint8Array, sig: Uint8Array, publicKey: Uint8Array): boolean { | ||
try { | ||
return nacl.sign.detached.verify(msg, sig, publicKey); | ||
} | ||
catch (error) { | ||
console.error(error); | ||
return false; | ||
} | ||
} | ||
|
||
export function uint8ArrayToBits(array: Uint8Array): Bit[] { | ||
const result: number[] = Array.from({ length: array.length * 8 }); | ||
for (const [i, byte] of array.entries()) | ||
for (let j = 0; j < 8; j++) | ||
result[i * 8 + j] = (byte >> j) & 1; | ||
|
||
return result as Bit[]; | ||
} | ||
|
||
export function bitGenerator(): () => Bit { | ||
const context = { | ||
consumed: 0, | ||
random: uint8ArrayToBits(rand(1)), | ||
}; | ||
|
||
const next = (): Bit => { | ||
if (context.consumed === 8) { | ||
context.random = uint8ArrayToBits(rand(1)); | ||
context.consumed = 0; | ||
} | ||
|
||
return context.random[context.consumed++]; | ||
}; | ||
|
||
return next; | ||
} | ||
|
||
export const bit = bitGenerator(); | ||
|
||
export function po2(num: bigint): bigint { | ||
num |= num >> 1n; | ||
num |= num >> 2n; | ||
num |= num >> 4n; | ||
num |= num >> 8n; | ||
num |= num >> 16n; | ||
num |= num >> 32n; | ||
num |= num >> 64n; | ||
return num + 1n; | ||
} |
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,58 @@ | ||
import type { Ascii85Standard } from '@std/encoding/ascii85'; | ||
import { decodeAscii85, encodeAscii85 } from '@std/encoding/ascii85'; | ||
import { decodeBase32, encodeBase32 } from '@std/encoding/base32'; | ||
import { decodeBase58, encodeBase58 } from '@std/encoding/base58'; | ||
import { decodeBase64, encodeBase64 } from '@std/encoding/base64'; | ||
import { decodeBase64Url, encodeBase64Url } from '@std/encoding/base64url'; | ||
import { decodeHex, encodeHex } from '@std/encoding/hex'; | ||
// import { decodeVarint, encodeVarint } from '@std/encoding/varint'; | ||
|
||
export type Encodable = ArrayBuffer | Uint8Array | string; | ||
export type Encoder = (data: Encodable) => string; | ||
export type Decoder = (source: string) => Uint8Array; | ||
export type StringDecoder = (source: string) => string; | ||
export type Base64Standard = 'btoa' | 'url' | 'url_padded'; | ||
|
||
const textDecoder = new TextDecoder(); | ||
const $pad_encodeBase64Url: Encoder = data => encodeBase64(data).replace(/\+/g, '-').replace(/\//g, '_'); | ||
const $pad_decodeBase64Url: Decoder = source => decodeBase64(source.replace(/-/g, '+').replace(/_/g, '/')); | ||
const $petal_encodeBase64 = (data: Encodable, standard: Base64Standard = 'btoa'): string => ({ btoa: encodeBase64, url: encodeBase64Url, url_padded: $pad_encodeBase64Url })[standard](data); | ||
const $petal_encodeAscii85 = (data: Encodable, standard?: Ascii85Standard): string => encodeAscii85(data, { standard }); | ||
const $petal_decodeBase64 = (source: string, standard: Base64Standard = 'btoa'): Uint8Array => ({ btoa: decodeBase64, url: decodeBase64Url, url_padded: $pad_decodeBase64Url })[standard](source); | ||
const $petal_decodeAscii85 = (source: string, standard?: Ascii85Standard): Uint8Array => decodeAscii85(source, { standard }); | ||
|
||
const encoders = { | ||
16: encodeHex, | ||
32: encodeBase32, | ||
58: encodeBase58, | ||
64: $petal_encodeBase64, | ||
85: $petal_encodeAscii85, | ||
}; | ||
|
||
const decoders = { | ||
16: decodeHex, | ||
32: decodeBase32, | ||
58: decodeBase58, | ||
64: $petal_decodeBase64, | ||
85: $petal_decodeAscii85, | ||
}; | ||
|
||
export type Base = Extract<keyof typeof encoders, number>; | ||
export type Variation<B extends Base> = NonNullable<Parameters<(typeof encoders)[B]>[1]>; | ||
export interface BaseProvider { encode: Encoder; decode: Decoder; decodeString: StringDecoder } | ||
|
||
export function base(base: Base): BaseProvider; | ||
export function base<B extends Base>(base: B, variation: Variation<B>): BaseProvider; | ||
export function base<B extends Base>(base: B, variation?: Variation<B>): BaseProvider { | ||
const encoder = encoders[base] as (data: Encodable, variation?: string) => string; | ||
const decoder = decoders[base] as (source: string, variation?: string) => Uint8Array; | ||
|
||
return { | ||
encode: data => encoder(data, variation), | ||
decode: source => decoder(source, variation), | ||
decodeString: source => textDecoder.decode(decoder(source, variation)), | ||
}; | ||
} | ||
|
||
export { base as b }; | ||
export default base; |
File renamed without changes.
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,3 @@ | ||
import { createTsupConfig } from '../scripts/tsup.config.ts'; | ||
|
||
export default createTsupConfig('crypto', {}); | ||
export default createTsupConfig('crypto', { iife: { disabled: true } }); |
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,3 @@ | ||
import { createVitestConfig } from '../scripts/vitest.config.ts'; | ||
|
||
export default createVitestConfig`crypto`; |
Oops, something went wrong.