Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"authly",
"bytewise",
"cloudly",
"crockford",
"cryptly",
"Encrypter",
"Encrypters",
Expand Down
26 changes: 0 additions & 26 deletions ArrayBuffer.ts

This file was deleted.

2 changes: 1 addition & 1 deletion ArrayBuffer.spec.ts → ArrayBuffer/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { cryptly } from "./index"
import { cryptly } from "../index"

describe("Base64", () => {
it("xor same length", () => {
Expand Down
46 changes: 46 additions & 0 deletions ArrayBuffer/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
export namespace ArrayBuffer {
function reduce(reducer: (left: number, right: number) => number, data: ArrayBuffer[]): ArrayBuffer {
let result = new Uint8Array(data.reduce((l, d) => (d.byteLength > l ? d.byteLength : l), 0))
for (const d of data.map(d => new Uint8Array(d))) {
const offset = result.length - d.length
result = result.reduceRight(
(r, value, index) => ((r[index] = index < offset ? value : reducer(value, d[index - offset])), r),
result
)
}
return result.buffer
}

export function xor(...data: ArrayBuffer[]): ArrayBuffer {
return reduce((left, right) => left ^ right, data)
}
export function bytewiseAdd(...data: ArrayBuffer[]): ArrayBuffer {
return reduce((left, right) => left + right, data)
}
export function add(...data: ArrayBuffer[]): ArrayBuffer {
let previous = 0
return reduce((left, right) => (previous = (previous >> 8) + left + right), data)
}
export function combine(...data: ArrayBuffer[]): ArrayBuffer {
let previous = 0
return reduce((left, right) => (previous = (left << 1) + (previous >> 8) + left + right), data)
}
export function random<
T extends
| Int8Array
| Int16Array
| Int32Array
| Uint8Array
| Uint16Array
| Uint32Array
| Uint8ClampedArray
| Float32Array
| Float64Array
| DataView
| null
>(array: T): T
export function random(length: number): Uint8Array
export function random(array: ArrayBufferView | number): ArrayBufferView {
return crypto.getRandomValues(typeof array == "number" ? new Uint8Array(length) : array)
}
}
37 changes: 0 additions & 37 deletions Base16.ts

This file was deleted.

12 changes: 7 additions & 5 deletions Base16.spec.ts → Base16/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { cryptly } from "./index"
import { cryptly } from "../index"

describe("Base16", () => {
it.each(["1337", "1ee7", "1EE7", "", "0", "ffff"])("is %s", value => expect(cryptly.Base16.is(value)).toEqual(true))
it.each(["13.37", "1eet", "1EET", "O"])("is not %s", value => expect(cryptly.Base16.is(value)).toEqual(false))
it("encode standard 1", () =>
expect(cryptly.Base16.encode("This is the data (*)")).toEqual("5468697320697320746865206461746120282a29"))
it("encode standard 2", () =>
Expand All @@ -10,19 +12,19 @@ describe("Base16", () => {
it("encode standard 4", () =>
expect(cryptly.Base16.encode("any carnal pleasur")).toEqual("616e79206361726e616c20706c6561737572"))
it("decode standard 1", () =>
expect(new cryptly.TextDecoder().decode(cryptly.Base16.decode("5468697320697320746865206461746120282a29"))).toEqual(
expect(new TextDecoder().decode(cryptly.Base16.decode("5468697320697320746865206461746120282a29"))).toEqual(
"This is the data (*)"
))
it("decode standard 2", () =>
expect(new cryptly.TextDecoder().decode(cryptly.Base16.decode("616e79206361726e616c20706c6561737572652e"))).toEqual(
expect(new TextDecoder().decode(cryptly.Base16.decode("616e79206361726e616c20706c6561737572652e"))).toEqual(
"any carnal pleasure."
))
it("decode standard 3", () =>
expect(new cryptly.TextDecoder().decode(cryptly.Base16.decode("616e79206361726e616c20706c656173757265"))).toEqual(
expect(new TextDecoder().decode(cryptly.Base16.decode("616e79206361726e616c20706c656173757265"))).toEqual(
"any carnal pleasure"
))
it("decode standard 4", () =>
expect(new cryptly.TextDecoder().decode(cryptly.Base16.decode("616e79206361726e616c20706c6561737572"))).toEqual(
expect(new TextDecoder().decode(cryptly.Base16.decode("616e79206361726e616c20706c6561737572"))).toEqual(
"any carnal pleasur"
))
it("xor", () =>
Expand Down
47 changes: 47 additions & 0 deletions Base16/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { isly } from "isly"
import { ArrayBuffer } from "../ArrayBuffer"

export type Base16 = string

export namespace Base16 {
export const type = isly.named<Base16>("cryptly.Base16", isly.string(/^[A-Fa-f0-9]*$/))
export const is = type.is
export const flaw = type.flaw
export function encode(value: ArrayBuffer | Uint8Array | string, length?: number): Base16 {
const data =
typeof value == "string"
? new TextEncoder().encode(value)
: value instanceof Uint8Array
? value
: new Uint8Array(value)
let result: string[] = []
for (const d of data)
result.push(Math.floor(d / 16).toString(16), (d % 16).toString(16))
if (length)
result = result.slice(0, length)
return result.join("")
}
export function decode(value: Base16): Uint8Array {
if (value.length % 2 == 1)
value += "0"
const result = new Uint8Array(value.length / 2)
for (let index = 0; index < result.length; index++)
result[index] = Number.parseInt(value[index * 2], 16) * 16 + Number.parseInt(value[index * 2 + 1], 16)
return result
}
export function xor(data: Base16[]): Base16 {
return encode(ArrayBuffer.xor(...data.map(decode)))
}
export function bytewiseAdd(data: Base16[]): Base16 {
return encode(ArrayBuffer.bytewiseAdd(...data.map(decode)))
}
export function add(data: Base16[]): Base16 {
return encode(ArrayBuffer.add(...data.map(decode)))
}
export function combine(data: Base16[]): Base16 {
return encode(ArrayBuffer.combine(...data.map(decode)))
}
export function random(length: number): Base16 {
return Base16.encode(ArrayBuffer.random(Math.ceil(length / 2))).substring(0, length)
}
}
77 changes: 0 additions & 77 deletions Base32.ts

This file was deleted.

9 changes: 9 additions & 0 deletions Base32/Standard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { isly } from "isly"

export type Standard = typeof Standard.values[number]
export namespace Standard {
export const values = ["standard", "hex", "crockford"] as const
export const type = isly.named<Standard>("cryptly.Base32.Standard", isly.string(values))
export const is = type.is
export const flaw = type.flaw
}
12 changes: 7 additions & 5 deletions Base32.spec.ts → Base32/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { cryptly } from "./index"
import { cryptly } from "../index"

describe("Base32", () => {
it.each(["1337", "LEET1337", "LEET", "", "0"])("is %s", value => expect(cryptly.Base32.is(value)).toEqual(true))
it.each(["13.37", "1eet1337"])("is not %s", value => expect(cryptly.Base32.is(value)).toEqual(false))
it("encode standard 1", () =>
expect(cryptly.Base32.encode("This is the data (*)")).toEqual("KRUGS4ZANFZSA5DIMUQGIYLUMEQCQKRJ"))
it("encode standard 2", () =>
Expand All @@ -10,19 +12,19 @@ describe("Base32", () => {
it("encode standard 4", () =>
expect(cryptly.Base32.encode("any carnal pleasur")).toEqual("MFXHSIDDMFZG4YLMEBYGYZLBON2XE"))
it("decode standard 1", () =>
expect(new cryptly.TextDecoder().decode(cryptly.Base32.decode("KRUGS4ZANFZSA5DIMUQGIYLUMEQCQKRJ"))).toEqual(
expect(new TextDecoder().decode(cryptly.Base32.decode("KRUGS4ZANFZSA5DIMUQGIYLUMEQCQKRJ"))).toEqual(
"This is the data (*)"
))
it("decode standard 2", () =>
expect(new cryptly.TextDecoder().decode(cryptly.Base32.decode("MFXHSIDDMFZG4YLMEBYGYZLBON2XEZJO"))).toEqual(
expect(new TextDecoder().decode(cryptly.Base32.decode("MFXHSIDDMFZG4YLMEBYGYZLBON2XEZJO"))).toEqual(
"any carnal pleasure."
))
it("decode standard 3", () =>
expect(new cryptly.TextDecoder().decode(cryptly.Base32.decode("MFXHSIDDMFZG4YLMEBYGYZLBON2XEZI"))).toEqual(
expect(new TextDecoder().decode(cryptly.Base32.decode("MFXHSIDDMFZG4YLMEBYGYZLBON2XEZI"))).toEqual(
"any carnal pleasure"
))
it("decode standard 4", () =>
expect(new cryptly.TextDecoder().decode(cryptly.Base32.decode("MFXHSIDDMFZG4YLMEBYGYZLBON2XE"))).toEqual(
expect(new TextDecoder().decode(cryptly.Base32.decode("MFXHSIDDMFZG4YLMEBYGYZLBON2XE"))).toEqual(
"any carnal pleasur"
))
it("xor", () =>
Expand Down
Loading