raaz-0.0.1: The raaz cryptographic library.

Safe HaskellNone
LanguageHaskell98

Raaz.Core.Encode

Contents

Synopsis

The encodable type

Many types like cryptographic hashes, secret keys etc can be encoded into bytes. This module gives an interface to such objects using the Encodable type class. To ease their printing most types of this class have a Show instances. Similarly, to make it easy to defines constants of these types in source code, they often are instances of IsString. Typically for cryptographic types like hashes, secret keys etc the Show and IsString instances correspond to the base-16 encoding of these types.

class Encodable a where Source #

The type class Encodable captures all the types can be encoding into ByteString.

Methods

toByteString :: a -> ByteString Source #

Convert stuff to bytestring

fromByteString :: ByteString -> Maybe a Source #

Try parsing back a value. Returns nothing on failure.

unsafeFromByteString :: ByteString -> a Source #

Unsafe version of fromByteString

toByteString :: EndianStore a => a -> ByteString Source #

Convert stuff to bytestring

fromByteString :: EndianStore a => ByteString -> Maybe a Source #

Try parsing back a value. Returns nothing on failure.

Instances

Encodable ByteString Source # 
Encodable Base16 Source # 
Encodable Write Source # 
Encodable SHA1 Source # 
Encodable SHA224 Source # 
Encodable SHA256 Source # 
Encodable SHA384 Source # 
Encodable SHA512 Source # 
Encodable IV Source # 
Encodable KEY256 Source # 
Encodable KEY192 Source # 
Encodable KEY128 Source # 
Encodable a => Encodable (BITS a) Source # 
Encodable a => Encodable (BYTES a) Source # 
Encodable (BE Word32) Source # 
Encodable (BE Word64) Source # 
Encodable (LE Word32) Source # 
Encodable (LE Word64) Source # 
Encodable h => Encodable (HMAC h) Source # 

Encoding formats

We also give facilities to encode any instance of Encodable into multiple formats. For type safety, encoding formats are distinguished by their types. All such formats have to be members of the Format type class and this allows encoding and decoding any type that is an instance of Encodable into any of the desired format.

class (IsString fmt, Show fmt, Encodable fmt) => Format fmt where Source #

A binary encoding format is something for which there is a 1:1 correspondence with bytestrings. We also insist that it is an instance of IsString, so that it can be easily included in source code, and Show, so that it can be easily printed out.

Minimal complete definition

encodeByteString, decodeFormat

Instances

encode :: (Encodable a, Format fmt) => a -> fmt Source #

Encode in a given format.

decode :: (Format fmt, Encodable a) => fmt -> Maybe a Source #

Decode from a given format. It results in Nothing if there is a parse error.

unsafeDecode :: (Format fmt, Encodable a) => fmt -> a Source #

The unsafe version of decode.

The base 16 encoding fromat

fromBase16 :: Encodable a => String -> a Source #

Base16 variant of fromString. Useful in definition of IsString instances as well as in cases where the default IsString instance does not parse from a base16 encoding.

showBase16 :: Encodable a => a -> String Source #

Base16 variant of show.