HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Modulecrypto-api-0.13.3Haskell98

Crypto.Util

A small selection of utilities that might be of use to others working with bytestring/number combinations.

  • 12 values
valuei2bs :: Int -> Integer -> ByteString
#

i2bs bitLen i converts i to a ByteString of bitLen bits (must be a multiple of 8).

i2bs_unsized i converts i to a ByteString of sufficient bytes to express the integer. The integer must be non-negative and a zero will be encoded in one byte.

valuethrowLeft :: Exception e => Either e a -> a
#

Useful utility to extract the result of a generator operation and translate error results to exceptions.

valuefor :: Tagged a b -> a -> b
#

Obtain a tagged value for a particular instantiated type.

Checks two bytestrings for equality without breaches for timing attacks.

Semantically, constTimeEq = (==). However, x == y takes less time when the first byte is different than when the first byte is equal. This side channel allows an attacker to mount a timing attack. On the other hand, constTimeEq always takes the same time regardless of the bytestrings' contents, unless they are of difference size.

You should always use constTimeEq when comparing secrets, otherwise you may leave a significant security hole (cf. http://codahale.com/a-lesson-in-timing-attacks/).

zipWith xor + Pack As a result of rewrite rules, this should automatically be optimized (at compile time). to use the bytestring libraries zipWith' function.

zipWith xor + Pack

This is written intentionally to take advantage of the bytestring libraries zipWith' rewrite rule but at the extra cost of the resulting lazy bytestring being more fragmented than either of the two inputs.