data
data
¶
Encoding, hashing, compression and byte packing.
This is pure CPU work with no GPU or window involved. It covers base64 and
hex, message digests (md5, sha1, sha256, sha512) returned as lowercase hex,
compression in the zlib, gzip and deflate formats, and a small integer
packer. Hex and base64 are reversible, digests are one-way, and the digest
proc is named digest rather than hash so it does not shadow
std/hashes.hash for code that imports both.
HashFunction
¶
type HashFunction = enum
The supported message-digest algorithms.
Values
-
hfMD5 -
hfSHA1 -
hfSHA256 -
hfSHA512
CompressFormat
¶
type CompressFormat = enum
The compressed container formats: zlib, gzip and raw deflate.
Values
-
compZlib -
compGzip -
compDeflate
encode ¶
proc encode(data: openArray[byte]; hex = false): string
Encode raw bytes as base64, or as lowercase hex when hex is true.
Parameters
-
data(openArray[byte]) -
hex(auto)
Returns
string
encode ¶
proc encode(s: string; hex = false): string
Encode a string's bytes as base64, or as hex when hex is true.
Parameters
-
s(string) -
hex(auto)
Returns
string
decode ¶
proc decode(s: string; hex = false): seq[byte]
Decode a base64 (or hex) string back to raw bytes.
Parameters
-
s(string) -
hex(auto)
Returns
seq[byte]
decodeString ¶
proc decodeString(s: string; hex = false): string
Decode base64 or hex to a string, for when the payload is known text.
Parameters
-
s(string) -
hex(auto)
Returns
string
digestRaw ¶
proc digestRaw(fn: HashFunction; data: openArray[byte]): seq[byte]
Return the raw digest bytes of data under the chosen hash function.
Parameters
-
fn(HashFunction) -
data(openArray[byte])
Returns
seq[byte]
digest ¶
proc digest(fn: HashFunction; data: openArray[byte]): string
Return the lowercase hex digest of data under the chosen hash function.
Parameters
-
fn(HashFunction) -
data(openArray[byte])
Returns
string
digest ¶
proc digest(fn: HashFunction; s: string): string
Return the lowercase hex digest of a string.
Parameters
-
fn(HashFunction) -
s(string)
Returns
string
compress ¶
proc compress(data: openArray[byte]; format = compZlib; level = -1): seq[byte]
Compress bytes with zlib, gzip or deflate. Level -1 is the default, 1 is
fastest and 9 is smallest.
Parameters
-
data(openArray[byte]) -
format(auto) -
level(auto)
Returns
seq[byte]
compress ¶
proc compress(s: string; format = compZlib; level = -1): seq[byte]
Compress a string's bytes.
Parameters
-
s(string) -
format(auto) -
level(auto)
Returns
seq[byte]
decompress ¶
proc decompress(data: openArray[byte]; format = compZlib): seq[byte]
Reverse compress for the given format.
Parameters
-
data(openArray[byte]) -
format(auto)
Returns
seq[byte]
decompressString ¶
proc decompressString(data: openArray[byte]; format = compZlib): string
Decompress to a string when the original payload was text.
Parameters
-
data(openArray[byte]) -
format(auto)
Returns
string
pack ¶
proc pack(format: string; args: varargs[int64]): seq[byte]
Pack integer args into bytes following a format string of width and
endianness codes.
Parameters
-
format(string) -
args(varargs[int64])
Returns
seq[byte]
unpack ¶
proc unpack(format: string; data: openArray[byte]): seq[int64]
Unpack bytes back into integers following the same format string used to
pack them.
Parameters
-
format(string) -
data(openArray[byte])
Returns
seq[int64]