Advanced cryptography for potatOS.
Go to file
dependabot[bot] ffedfa3059
Bump c8 from 9.1.0 to 10.1.2 (#36)
Bumps [c8](https://github.com/bcoe/c8) from 9.1.0 to 10.1.2.
- [Release notes](https://github.com/bcoe/c8/releases)
- [Changelog](https://github.com/bcoe/c8/blob/main/CHANGELOG.md)
- [Commits](https://github.com/bcoe/c8/compare/v9.1.0...v10.1.2)

---
updated-dependencies:
- dependency-name: c8
  dependency-type: direct:development
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2024-07-01 16:46:43 +01:00
.github Update workflow-1.yml (#33) 2023-08-05 14:42:42 +01:00
src ES modules (#24) 2021-10-15 20:12:42 +01:00
test ES modules (#24) 2021-10-15 20:12:42 +01:00
.editorconfig Omegaupdate 2020-01-29 23:32:06 +00:00
.gitattributes 🍭 Added .gitattributes & .gitignore files 2016-04-02 16:20:05 +01:00
.gitignore ES modules (#24) 2021-10-15 20:12:42 +01:00
CHANGELOG.md ES modules (#24) 2021-10-15 20:12:42 +01:00
LICENSE.txt Create LICENSE.txt 2021-05-17 20:06:51 +01:00
README.md Update README.md 2022-08-17 23:06:41 +01:00
package-lock.json Bump c8 from 9.1.0 to 10.1.2 (#36) 2024-07-01 16:46:43 +01:00
package.json Bump c8 from 9.1.0 to 10.1.2 (#36) 2024-07-01 16:46:43 +01:00

README.md

base1

Base1 encodes arbitrary binary data as a string of repeated "A" characters. With analogy to the unary numeral system, the binary data is encoded in the length of the string. This JavaScript module, base1, is the first implementation of the Base1 encoding.

An empty input becomes the empty string. The single byte 0x00 becomes "A". 0x01 becomes "AA". 0x02 becomes "AAA". 0xFF becomes

AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

(256 As). 0x00 0x00 becomes

AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

(257 As), and so on.

Base1 is not the most inefficient possible binary encoding, but it is a step in that direction: 1MB of input yields approximately 28,388,580MB of output. (Twice that, if using UTF-16.)

How it works

The Base1 encoding is not as simple as taking the binary as a place-value base 256 number. This would give no way to distinguish buffers with leading null bytes from one another. We have to encode the length of the source buffer as well. We do this by sorting all possible buffers by length and then lexicographically, then simply returning the index of the buffer in the list.

Buffer length Buffers of this length Minimum length of Base1 Maximum length of Base1
0 bytes 1 0 0
1 byte 256 1 256
2 bytes 65,536 257 65,792
3 bytes 16,777,216 65,793 16,843,008
4 bytes 4,294,967,296 16,843,009 4,311,810,304
... ... ... ...
N bytes 256N (256N - 1) / (256 - 1) (256N + 1 - 1) / (256 - 1) - 1

Installation

npm install base1

Usage

import { encodeL, encode, decodeL, decode } from 'base1'

const uint8Array = Uint8Array.from([0x03, 0xC0])

const l   = encodeL(uint8Array) // 1217n
const str = encode(uint8Array)  // "AAAAAAAAAAAA...AA", string is 1,217 characters long

const uint8Array2 = decode(str) // Uint8Array [ 3, 192 ]
const uint8Array3 = decodeL(l)  // Uint8Array [ 3, 192 ]

API

base1 accepts and returns Uint8Arrays. Note that every Node.js Buffer is a Uint8Array. A Uint8Array can be converted to a Node.js Buffer like so:

const buffer = Buffer.from(uint8Array.buffer, uint8Array.byteOffset, uint8Array.byteLength)

encodeL(uint8Array)

Input a Uint8Array. Returns a Base1 string length, in the form of a BigInt.

encode(uint8Array)

Encodes a Uint8Array as a Base1 string. This method calls base1.encodeL to get a length l and then returns a string which is l repetitions of "A" in a row.

JavaScript does not specify, nor does base1 enforce, a maximum length for a string. However, MDN's polyfill for String.prototype.repeat gives an upper limit of 228 - 1 = 268,435,455 characters. This is equivalent to the 4-byte sequence 0x0E 0xFE 0xFE 0xFE. Buffers which are longer or lexicographically greater than this may cause errors in your JavaScript engine.

decodeL(l)

Take a Base1 string length in the form of a BigInt and return a Uint8Array containing the data which it represents.

decode(str)

Decode a Base1 string and return a Uint8Array containing the data which it represents.

Ports

This is a JavaScript implementation of the Base1 encoding. It has also been ported: