File: KeySelection.hs

package info (click to toggle)
haskell-hopenpgp 2.10.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,028 kB
  • sloc: haskell: 6,478; sh: 21; makefile: 6
file content (46 lines) | stat: -rw-r--r-- 1,307 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
-- KeySelection.hs: OpenPGP (RFC4880) ways to ask for keys
-- Copyright © 2014-2018  Clint Adams
-- This software is released under the terms of the Expat license.
-- (See the LICENSE file).
{-# LANGUAGE OverloadedStrings #-}

module Codec.Encryption.OpenPGP.KeySelection
  ( parseEightOctetKeyId
  , parseFingerprint
  ) where

import Codec.Encryption.OpenPGP.Types
import Control.Applicative (optional)
import Control.Monad ((<=<))
import Crypto.Number.Serialize (i2osp)
import Data.Attoparsec.Text
  ( Parser
  , asciiCI
  , count
  , hexadecimal
  , inClass
  , parseOnly
  , satisfy
  )
import qualified Data.ByteString.Lazy as BL
import Data.Text (Text, toUpper)
import qualified Data.Text as T

parseEightOctetKeyId :: Text -> Either String EightOctetKeyId
parseEightOctetKeyId =
  fmap EightOctetKeyId .
  (parseOnly hexes <=< parseOnly (hexPrefix *> hexen 16)) . toUpper

parseFingerprint :: Text -> Either String TwentyOctetFingerprint
parseFingerprint =
  fmap TwentyOctetFingerprint .
  (parseOnly hexes <=< parseOnly (hexen 40)) . toUpper . T.filter (/= ' ')

hexPrefix :: Parser (Maybe Text)
hexPrefix = optional (asciiCI "0x")

hexen :: Int -> Parser Text
hexen n = T.pack <$> count n (satisfy (inClass "A-F0-9"))

hexes :: Parser BL.ByteString
hexes = BL.fromStrict . i2osp <$> hexadecimal