    -- Fingerprint.hs: OpenPGP (RFC4880) fingerprinting methods
    -- Copyright © 2012-2013  Clint Adams
    -- This software is released under the terms of the Expat license.
    -- (See the LICENSE file).

    module Codec.Encryption.OpenPGP.Fingerprint (
       eightOctetKeyID
     , fingerprint
    ) where

    import qualified Crypto.PubKey.RSA as RSA
    import qualified Crypto.Hash.MD5 as MD5
    import qualified Crypto.Hash.SHA1 as SHA1
    import qualified Data.ByteString as B
    import Data.Serialize.Put (runPut)

    import Codec.Encryption.OpenPGP.SerializeForSigs (putPKPforFingerprinting)
    import Codec.Encryption.OpenPGP.Internal (integerToBEBS)
    import Codec.Encryption.OpenPGP.Types

    eightOctetKeyID :: PKPayload -> EightOctetKeyId
    eightOctetKeyID (PKPayload DeprecatedV3 _ _ RSA (RSAPubKey rp)) = (EightOctetKeyId . B.reverse . B.take 4 . B.reverse . integerToBEBS . RSA.public_n) rp
    eightOctetKeyID p4@(PKPayload V4 _ _ _ _) = (EightOctetKeyId . B.drop 12 . unTOF . fingerprint) p4
    eightOctetKeyID _ = error "This should never happen."

    fingerprint :: PKPayload -> TwentyOctetFingerprint
    fingerprint p3@(PKPayload DeprecatedV3 _ _ _ _) = (TwentyOctetFingerprint . MD5.hash) (runPut $ putPKPforFingerprinting (PublicKeyPkt p3))
    fingerprint p4@(PKPayload V4 _ _ _ _) = (TwentyOctetFingerprint . SHA1.hash) (runPut $ putPKPforFingerprinting (PublicKeyPkt p4))