0.7.16 • Published 5 months ago

gnablib v0.7.16

Weekly downloads
-
License
MPL-1.1
Repository
github
Last release
5 months ago

gnablib

A lean, zero dependency library to provide a useful base for your project. Checksums, cryptography, codecs, date-times, error-checking-codes, logging, pseudorandom number generation. The tools you need for any project. Secure build pipeline, provenance signed and typed.

CI NPM ver install size deps LoC npm.io

Contents

Installation

This module is available through NPM. There are no install scripts, decrease your risk by explicitly preventing (--ignore-scripts)

(pnpm|npm) install gnablib --ignore-scripts

To verify provenance:

npm audit signatures

Details

Augmented Backus-Naur Form /rfc5234

Algorithms

Checksum/Hashsum

A checksum; can be used to prevent/identify accidental changes.

NameSum size bits
Adler3232
Block Check Character8
Cksum32
CRC2424
CRC3232
Fletcher1616
Fletcher3232
Fletcher6464
Longitudinal Redundancy Check8

The Luhn algorithm is also support, which only works on integers/numerical digits (eg. credit cards, SI numbers)

A hash; maps some data to another, often used for hash tables, or to speed up comparison. We use the term hashsum to distinguish from cryptographic-hashes (although MD5 and SHA1 were once cryptographically safe)

NameSum size bitsOptional parameters
Lookup232seed
Lookup332+32/64seed
MD5Sum128
Murmur332seed
SHA1Sum160
Spooky v2128seed
xxHash3232seed
xxHash6464seed

Command Line Interface (cli)

  • Argument and option parsing
  • Foreground, background color (8 color, 213 color, 16M colors)
  • Underline, bold, faint, italic, blink, invert, hide, strike-through, overline text styles
  • Terminal controls & style (cursor move, screen/line clear)

Codec

Crypto

Block

CBC or CTR are recommended by Niels Ferguson and Bruce Schneier. ECB should not be used.

Padding

Symmetric Encryption

NameBlock sizeKey sizeNonce sizeFeatures
Advanced Encryption Standard (AES)1616, 24, 320
Ascon-12881616AEAD
Ascon-128a161616AEAD
Ascon-80pq82016AEAD, Resistance to quantum adversary
Blowfish84-560
ChaCha206416, 3212
ChaCha20-Poly1305643212AEAD
Rabbit16160, 8
Salsa206416, 328
Salsa20-Poly1305643212AEAD
Twofish1616, 24, 320
XChaCha20643224
XChaCha20-Poly1305643224AEAD
XSalsa206416, 3224
XSalsa20-Poly1305643224AEAD

Secure Hash

Cryptography hash functions that have the properties:

  • Finding an input string that matches a hash value (pre-image) is hard
  • Finding a pair of messages that generate the same hash value (collision) is hard
NameDigest sizesOptional parameters
Ascon-Hash, Ascon-HashA256-
Blake256, 512salt
Blake2b8-512, 256, 384, 512key, salt, personalization
Blake2s8-256, 224, 256key, salt, personalization
Keccak8-512, 224, 256, 384, 512capacity
MD4†128†-
MD5†128†-
ParallelHash (128,256)8-512block size, customization
RipeMD†128†, 160†, 256, 320-
SHA-1†160†-
SHA-2224, 256, 384, 512, 512/224, 512/256-
SHA-3224, 256, 384, 512-
TupleHash (128, 256)8-512customization
Whirlpool512-

† No longer considered cryptographically safe

Key Derivation Function (KDF)

A cryptographic algorithm that derives one or more secret keys from a secret value. Can be used to stretch keys (make them longer), or obtain a key in a particular format (eg making a key a fixed length)

  • HKDF
  • PBKDF2 pbkdf2_hmac_sha1, pbkdf2_hmac_sha256, pbkdf2_hmac_sha512

Message Authentication Code (MAC)

Also known as an authentication tag, a short piece of information to authenticate a message. Protect integrity, and authenticity.

NameTag Size
CMAC16 (AES)
HMACDepends on hash (Blake, Blake2, cShake, Keccak, MD4, MD5, RipeMD, Sha1, Sha2, Sha3, Shake, Whirlpool)
HopMAC16 (Kangaroo Twelve)
KMAC16, 32 (Keccak)
Poly130516

EXtendable-Output Function (XOF)

A secure hash that can produce output of any desired length.

NameCapacitiesOptional parameters
Ascon-Xof, Ascon-XofA256digest size
cShake128, 256digest size, function name, customization
KangarooTwelve128digest size, customization
KmacXof128, 256digest size, key, customization
ParallelHashXof128, 256block size, digest size, customization
Shake128, 256digest size
TupleHashXof128, 256digest size, customization
TurboSHAKE128, 256digest size, customization

DateTime

  • Year, Month, Day
  • Hour, Minute, Second, Millisecond, Microsecond
  • DateTimeLocal, DateTimeUtc, DateOnly, TimeOnly
  • Duration, DurationExact
  • DateTime*.lt|lte|eq|gt|gte|neq - Compare two date-times
  • DateTime*.add|sub - Add or subtract Duration|DurationExact from a date-time
  • DateTime.diff|diffExact - Get the difference between two dates in y/m/d/h/ or d/h/*

Error Correction Codes

Endian

  • Convert between big (BE) and little (LE) Endian encoding 16/32/64/128 bits, or a stream of bytes.
  • Detects platform endianness

Big endian is closer to the way we write numbers left-to-right in English. 1726 = 1x1000, 7x100, 2x10, 6x1

Little endian is the dominant ordering for processor architectures including; x86 and RISC-V, ARM can be either but defaults to little. Famous big endian processors include PDP-10, Motorola 68000 series (early Macintosh, Amiga, Atari, Sega), IBM Z and SPARC, which can be either but defaults to big.

Primitive

  • BitReader / BitWriter
  • Fixed TypedArray
  • Lazy
  • Network CIDR, IPv4
  • Readonly TypedArray
  • Scaling TypedArray
  • StringBuilder
  • WindowStr
  • 16 bit: uint (U16)
  • 32 bit: uint (U32), mutable uint (U32Mut), mutable uint array (U32MutArray)
  • 64 bit: int (I64), uint (U64), mutable int (I64Mut), mutable uint (U64Mut), mutable uint array (U64MutArray)
  • 128 bit: uint (U128), mutable uint (U128Mut)
  • 256 bit: uint (U256)
  • 512 bit: uint (U512)

Extensions:

  • Bit (countBitsSet, lsbMask, reverse, countLeadZeros, nextPow2)
  • Float (parseDec)
  • Int (parseDec, parseHex, parseCsv)
  • String (splitChars, splitLen, reverse, padStart, filter, ctEq, ctSelect)
  • Uint (parseDec, parseHex, sign8, sign16, sign32, glScaleSize, toGlScaleBytes, fromGlScaleBytes)
  • Uint8Array (toGlBytes, ctEq, ctSelect, incrBE, lShiftEq, xorEq)

PseudoRandom number generators (PRNG)

Good PRNGs

Use one of these if you can, alphabetically sorted. gjrand32b is only somewhat official and doesn't have a lot of testing. B=Bytes, b=bits

NameYearVariantState BOut bSafe b
GJRand2005~gjrand32b1632
gjrand643264
Permuted Congruential Generator (PCG)2014pcg32832
pcg641664
Small Fast Counting (SFC)2010sfc16816
sfc321632
sfc643264
SplitMix2014splitmix32432
splitmix64864
Tychei2011tychei1632
Well equidistributed long-period linear (WELL)2006well5126432
XoRoShiRo2018xoroshiro64*832
xoroshiro64**832
xoroshiro128+166453
xoroshiro128++1664
xoroshiro128**1664
XorShift+2003xorshift128+1664
XoShiRo2018xoshiro128+163224
xoshiro128++1632
xoshiro128**1632
xoshiro256+326453
xoshiro256++3264
xoshiro256**3264

‡: Use U64 library to exceed JS 32bit integer constraint (may be slower)

U16 : 0 - 32767 | 0xFFFF
U31 : 0 - 2147483647 | 0x7FFFFFFF
U32 : 0 - 4294967295 | 0xFFFFFFFF
U64 : 0 - 18446744073709551615 | 0xFFFFFFFFFFFFFFFF

Poor PRNGs

Best not to use these, there are some dragons in these waters (so if you're choosing, be aware of the limitations). However, you might have a compatibility constraint (or love the name "Mersenne Twister").

NameYearVariantState BOut b
ARC4/RC4/Arcfour/Arc4random1997arc4328
Mersenne Twister1998mt19937249632
MulBerry322017mulberry32432
XorShift2003xorshift1281632

Bad PRNGs

If you're curious about the lineage, or need for compatibility with an old decision, but don't chose one of these. If you're already using one of these, consider migrating.

NameVariantState BOut b
Lehmer/LCG/MCG 1988all431
Marsaglia-14
Middle-Square-4n digits
MSVC (LCG)-416
RANDU (LCG)-431
XorShift 2003xorshift32432
xorshift64864

RegExp

  • RegExp escape string

Run time

  • Configuration (including collecting from environment variables)
  • Structured logging
  • Normalize stack entries, and stack traces across engines, with colors

License

Copyright © 2022-2025 gnablib contributors

MPL-1.1

0.4.42

7 months ago

0.4.43

7 months ago

0.4.48

7 months ago

0.4.49

7 months ago

0.4.46

7 months ago

0.4.47

7 months ago

0.4.44

7 months ago

0.4.45

7 months ago

0.7.2

6 months ago

0.7.1

6 months ago

0.7.4

6 months ago

0.7.3

6 months ago

0.7.0

6 months ago

0.7.11

5 months ago

0.7.10

5 months ago

0.7.13

5 months ago

0.7.12

5 months ago

0.7.15

5 months ago

0.7.14

5 months ago

0.7.16

5 months ago

0.5.0

6 months ago

0.5.1

6 months ago

0.7.9

5 months ago

0.7.6

6 months ago

0.7.5

6 months ago

0.7.8

6 months ago

0.7.7

6 months ago

0.4.53

6 months ago

0.4.54

6 months ago

0.4.51

7 months ago

0.4.52

6 months ago

0.4.50

7 months ago

0.4.55

6 months ago

0.6.1

6 months ago

0.6.0

6 months ago

0.4.41

1 year ago

0.4.40

1 year ago

0.4.39

1 year ago

0.4.38

1 year ago

0.4.37

1 year ago

0.4.36

1 year ago

0.4.35

1 year ago

0.4.34

1 year ago

0.4.33

1 year ago

0.4.32

1 year ago

0.4.31

1 year ago

0.4.30

1 year ago

0.4.29

1 year ago

0.4.28

1 year ago

0.4.27

1 year ago

0.4.26

1 year ago

0.4.25

1 year ago

0.4.24

1 year ago

0.4.23

1 year ago

0.4.21

1 year ago

0.4.22

1 year ago

0.4.20

1 year ago

0.4.19

1 year ago

0.4.18

1 year ago

0.4.17

1 year ago

0.4.16

1 year ago

0.4.15

1 year ago

0.4.14

1 year ago

0.4.13

1 year ago

0.4.12

1 year ago

0.4.11

1 year ago

0.4.10

1 year ago

0.4.9

1 year ago

0.4.8

1 year ago

0.4.5

1 year ago

0.4.4

1 year ago

0.4.7

1 year ago

0.4.6

1 year ago

0.4.0

1 year ago

0.4.2

1 year ago

0.3.8

1 year ago

0.3.7

1 year ago

0.3.6

1 year ago

0.3.5

1 year ago

0.3.3

2 years ago

0.1.10

2 years ago

0.1.11

2 years ago

0.1.12

2 years ago

0.2.27

2 years ago

0.2.26

2 years ago

0.2.25

2 years ago

0.2.24

2 years ago

0.2.23

2 years ago

0.2.22

2 years ago

0.2.21

2 years ago

0.2.18

2 years ago

0.2.17

2 years ago

0.2.30

2 years ago

0.1.20

2 years ago

0.1.21

2 years ago

0.2.16

2 years ago

0.2.15

2 years ago

0.2.14

2 years ago

0.2.13

2 years ago

0.2.12

2 years ago

0.2.11

2 years ago

0.2.10

2 years ago

0.2.1

2 years ago

0.2.0

2 years ago

0.2.29

2 years ago

0.2.28

2 years ago

0.2.7

2 years ago

0.1.16

2 years ago

0.1.8

2 years ago

0.1.17

2 years ago

0.1.7

2 years ago

0.2.9

2 years ago

0.1.18

2 years ago

0.2.8

2 years ago

0.1.9

2 years ago

0.3.2

2 years ago

0.2.3

2 years ago

0.3.1

2 years ago

0.2.2

2 years ago

0.2.5

2 years ago

0.1.6

2 years ago

0.2.4

2 years ago

0.1.5

2 years ago

0.1.4

2 years ago

0.1.3

2 years ago

0.1.2

2 years ago