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

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

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.Types

eightOctetKeyID :: PKPayload -> EightOctetKeyId
eightOctetKeyID = EightOctetKeyId . B.drop 12 . unTOF . fingerprint

fingerprint :: PKPayload -> TwentyOctetFingerprint
fingerprint (PubV4 ts pka pk) = (TwentyOctetFingerprint . SHA1.hash) (runPut $ putPKPforFingerprinting (PublicKey (PubV4 ts pka pk)))
fingerprint _ = error "non-V4 not implemented"