bencoding-lens: Lenses for bencoded data.

[ bsd3, data, library ] [ Propose Tags ]

A port of lens-aeson for bencoding.

[Skip to Readme]


[Index] [Quick Jump]


Maintainer's Corner

For package maintainers and hackage trustees


  • No Candidates
Versions [RSS]
Change log
Dependencies base (>=4.4 && <4.18), bencoding (>= && <0.5), bytestring (>= && <0.12), lens (>=4 && <5.2) [details]
License BSD-3-Clause
Copyright (c) 2021-2022, Joseph Morag
Author Joseph Morag
Maintainer Joseph Morag <>
Category Data
Home page
Bug tracker
Source repo head: git clone
Uploaded by jmorag at 2022-03-10T20:25:22Z
Distributions NixOS:
Downloads 25 total (1 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs uploaded by user
Build status unknown [no reports yet]

Readme for bencoding-lens-

[back to package description]


Law abiding lenses and traversals for bencoding, ported from lens-aeson. Works with arbitrary bencoded data, although most examples in the wild are torrent files.

{-# LANGUAGE OverloadedStrings #-}

import Control.Lens
import Data.BEncode
import Data.BEncode.Lens

import Network.HTTP.Simple -- from http-conduit
import Crypto.Hash -- from cryptonite

main = do
  -- Get the ubuntu 20.04 live server torrent file with http-conduit
  torrent <- getResponseBody <$> httpBS ""
  -- Print some interesting bits of the torrent file

  -- Announce link
  print (torrent ^? key "announce") 
  -- Just (BString "")

  -- Keys of the top level dict
  print (fmap fst (torrent ^@.. members))
  -- ["announce","announce-list","comment","created by","creation date","info"]

  -- Keys of the info dict
  print (fmap fst (torrent ^@.. key "info" . members))
  -- ["length","name","piece length","pieces"]

  -- Piece length
  print (torrent ^? key "info" . key "piece length" . _BInteger)
  -- Just 262144

  -- Compute the info hash (see definition below)
  print (torrent ^?! infoHash)
  -- b44a0e20fa5b7cecb77156333b4268dfd7c30afb
infoHash :: (AsBValue t) => Fold t (Digest SHA1)
infoHash = key "info" . to (hashlazy . encode)