opendht-hs-0.1.1.1: Haskell bindings for OpenDHT
Copyright(c) Simon Désaulniers 2025
LicenseGPL-3
Maintainersim.desaulniers@gmail.com
Safe HaskellSafe-Inferred
LanguageHaskell2010

OpenDHT.InfoHash

Description

This module exposes the different functions used to manipulate an OpenDHT InfoHash. An InfoHash encapsulates the result of the computation of the SHA-1 function. It's possible to derive such data from either a string, bytes or by picking from random.

Synopsis

The data type

data InfoHash Source #

This type represents a 160-bit string of bytes consistent with the SHA1 specification.

It is at the heart of this library and is used as arguments for the get/put functions.

Instances

Instances details
Show InfoHash Source # 
Instance details

Defined in OpenDHT.Internal.InfoHash

Eq InfoHash Source # 
Instance details

Defined in OpenDHT.Internal.InfoHash

Ord InfoHash Source # 
Instance details

Defined in OpenDHT.Internal.InfoHash

Initialization

emptyInfoHash :: Dht InfoHash Source #

Yields the empty InfoHash.

>>> unDht emptyInfoHash
0000000000000000000000000000000000000000

randomInfoHash :: Dht InfoHash Source #

Computes a random hash.

>>> unDht randomInfoHash
974b3ea26b91ab5c8155a6cca5c2a3c3a7645f58

infoHashFromHex :: String -> Dht InfoHash Source #

Create an InfoHash from a 40 character hexstring.

>>> unDht (infoHashFromHex "ffffffffffffffffffffffffffffffffffffffff")
ffffffffffffffffffffffffffffffffffffffff

infoHashFromBytes :: ByteString -> Dht InfoHash Source #

Computes the SHA-1 sum of a given ByteString and yield the resulting InfoHash.

>>> unDht (infoHashFromBytes (Data.ByteString.Char8.pack "0"))
b6589fc6ab0dc82cf12099d1c2d40ab994e8410c

infoHashFromString :: String -> Dht InfoHash Source #

Computes the SHA-1 sum of a given String and yield the resulting InfoHash.

>>> unDht (infoHashFromString "0")
b6589fc6ab0dc82cf12099d1c2d40ab994e8410c

Other

isZero :: InfoHash -> Dht Bool Source #

Tells whether infohash is the empty InfoHash.

>>> unDht (infoHashFromHex "" >>= isZero)
True
>>> unDht (infoHashFromString "0" >>= isZero)
False
>>> unDht (emptyInfoHash >>= isZero)
True