class std::BufCrypto

sys::Obj
  std::BufCrypto
crc

static extension Int crc(Buf buf, Str algorithm)

Compute a cycle reduancy check code using this buffer's contents from 0 to size. The supported algorithm names:

  • "CRC-16": also known as CRC-16-ANSI, CRC-16-IBM; used by USB, ANSI X3.28, and Modbus
  • "CRC-32": used by Ethernet, MPEG-2, PKZIP, Gzip, PNG
  • "CRC-32-Adler": used by Zlib

Raise ArgErr is algorithm is not available. This method is only supported for memory based buffers.

fromBase64

static Buf fromBase64(Str s)

Decode the specified Base64 string into its binary contents as defined by MIME RFC 2045. Any characters which are not included in the Base64 character set are safely ignored.

Example:

Buf.make.print("Fan").toBase64    => "RmFu"
Buf.fromBase64("RmFu").readAllStr => "Fan"
hmac

static extension Buf hmac(Buf buf, Str algorithm, Buf key)

Generate an HMAC message authentication as specified by RFC 2104. This buffer is the data input, algorithm specifies the hash digest, and key represents the secret key:

  • H: specified by algorthim parameter - "MD5" or "SHA1"
  • K: secret key specified by key parameter
  • B: fixed at 64
  • text: this instance

The HMAC is computed using:

ipad = the byte 0x36 repeated B times
opad = the byte 0x5C repeated B times
H(K XOR opad, H(K XOR ipad, text))

Throw ArgErr if the algorithm is not available. This method is only supported for memory buffers.

Examples:

"hi there".toBuf.hmac("MD5", "secret".toBuf)
pbk

static Buf pbk(Str algorithm, Str password, Buf salt, Int iterations, Int keyLen)

Generate a password based cryptographic key. Supported algoriths:

  • "PBKDF2WithHmacSHA1"
  • "PBKDF2WithHmacSHA256"

Parameters:

  • password: secret used to generate resulting cryptographic key
  • salt: cryptographic salt
  • iterations: number of iterations (the c term)
  • keyLen: desired length of key in bytes (not bits!)

Throw ArgErr if the algorithm is not available. This method is only supported for memory buffers.

toBase64

static extension Str toBase64(Buf buf)

Encode the buffer contents from 0 to size to a Base64 string as defined by MIME RFC 2045. No line breaks are added. This method is only supported by memory backed buffers, file backed buffers will throw UnsupportedErr.

Example:

Buf.make.print("Fan").toBase64    => "RmFu"
Buf.fromBase64("RmFu").readAllStr => "Fan"
toBase64Uri

static extension Str toBase64Uri(Buf buf)

Encode the buffer contents from 0 to size to a Uri-safe Base64 string as defined by RFC 4648. This means + is encoded as -, and / is encoded as _. Additionally, no padding is applied. This method is only supported by memory-backed buffers; file-backed buffers will throw UnsupportedErr.

Example:

Buf.make.print("safe base64~~").toBase64    => "c2FmZSBiYXNlNjR+fg=="
Buf.make.print("safe base64~~").toBase64Uri => "c2FmZSBiYXNlNjR-fg"
toDigest

static extension Buf toDigest(Buf buf, Str algorithm)

Apply the specified message digest algorthm to this buffer's contents from 0 to size and return the resulting hash. Digests are secure one-way hash functions which input an arbitrary sized buffer and return a fixed sized buffer. Common algorithms include: "MD5", "SHA-1", and "SHA-256"; the full list supported is platform dependent. On the Java VM, the algorithm maps to those avaialble via the java.security.MessageDigest API. Throw ArgErr if the algorithm is not available. This method is unsupported for mmap buffers.

Example:

Buf.make.print("password").print("salt").toDigest("MD5").toHex
 =>  "b305cadbb3bce54f3aa59c64fec00dea"