remove recently added cache from KeyVariety

Adding that field broke the Read/Show serialization back-compat,
and also the Eq and Ord instances were not blinded to it, which broke
git annex fsck and probably more.

I think that the new approach used in formatKeyVariety will be nearly
as fast, but have not benchmarked it.
This commit is contained in:
Joey Hess 2019-01-16 16:33:08 -04:00
parent 96aba8eff7
commit c3afb3434d
No known key found for this signature in database
GPG key ID: DB12DB0FF05F8F38
3 changed files with 112 additions and 111 deletions

View file

@ -37,7 +37,7 @@ exportKey sha = mk <$> catKey sha
mk (Just k) = AnnexKey k mk (Just k) = AnnexKey k
mk Nothing = GitKey $ Key mk Nothing = GitKey $ Key
{ keyName = encodeBS $ Git.fromRef sha { keyName = encodeBS $ Git.fromRef sha
, keyVariety = SHA1Key (HasExt False) mempty , keyVariety = SHA1Key (HasExt False)
, keySize = Nothing , keySize = Nothing
, keyMtime = Nothing , keyMtime = Nothing
, keyChunkSize = Nothing , keyChunkSize = Nothing

View file

@ -74,15 +74,15 @@ genBackendE hash = (genBackend hash)
} }
hashKeyVariety :: Hash -> HasExt -> KeyVariety hashKeyVariety :: Hash -> HasExt -> KeyVariety
hashKeyVariety MD5Hash he = MD5Key he mempty hashKeyVariety MD5Hash he = MD5Key he
hashKeyVariety SHA1Hash he = SHA1Key he mempty hashKeyVariety SHA1Hash he = SHA1Key he
hashKeyVariety (SHA2Hash size) he = SHA2Key size he mempty hashKeyVariety (SHA2Hash size) he = SHA2Key size he
hashKeyVariety (SHA3Hash size) he = SHA3Key size he mempty hashKeyVariety (SHA3Hash size) he = SHA3Key size he
hashKeyVariety (SkeinHash size) he = SKEINKey size he mempty hashKeyVariety (SkeinHash size) he = SKEINKey size he
#if MIN_VERSION_cryptonite(0,23,0) #if MIN_VERSION_cryptonite(0,23,0)
hashKeyVariety (Blake2bHash size) he = Blake2bKey size he mempty hashKeyVariety (Blake2bHash size) he = Blake2bKey size he
hashKeyVariety (Blake2sHash size) he = Blake2sKey size he mempty hashKeyVariety (Blake2sHash size) he = Blake2sKey size he
hashKeyVariety (Blake2spHash size) he = Blake2spKey size he mempty hashKeyVariety (Blake2spHash size) he = Blake2spKey size he
#endif #endif
{- A key is a hash of its contents. -} {- A key is a hash of its contents. -}

View file

@ -29,20 +29,16 @@ data Key = Key
newtype AssociatedFile = AssociatedFile (Maybe FilePath) newtype AssociatedFile = AssociatedFile (Maybe FilePath)
deriving (Show, Eq, Ord) deriving (Show, Eq, Ord)
{- There are several different varieties of keys. {- There are several different varieties of keys. -}
-
- The trailing ByteString can either be empty, or contain a cached
- formatting of the KeyVariety, in the form generated by formatKeyVariety.
-}
data KeyVariety data KeyVariety
= SHA2Key HashSize HasExt S.ByteString = SHA2Key HashSize HasExt
| SHA3Key HashSize HasExt S.ByteString | SHA3Key HashSize HasExt
| SKEINKey HashSize HasExt S.ByteString | SKEINKey HashSize HasExt
| Blake2bKey HashSize HasExt S.ByteString | Blake2bKey HashSize HasExt
| Blake2sKey HashSize HasExt S.ByteString | Blake2sKey HashSize HasExt
| Blake2spKey HashSize HasExt S.ByteString | Blake2spKey HashSize HasExt
| SHA1Key HasExt S.ByteString | SHA1Key HasExt
| MD5Key HasExt S.ByteString | MD5Key HasExt
| WORMKey | WORMKey
| URLKey | URLKey
-- Some repositories may contain keys of other varieties, -- Some repositories may contain keys of other varieties,
@ -59,38 +55,38 @@ newtype HashSize = HashSize Int
deriving (Eq, Ord, Read, Show) deriving (Eq, Ord, Read, Show)
hasExt :: KeyVariety -> Bool hasExt :: KeyVariety -> Bool
hasExt (SHA2Key _ (HasExt b) _) = b hasExt (SHA2Key _ (HasExt b)) = b
hasExt (SHA3Key _ (HasExt b) _) = b hasExt (SHA3Key _ (HasExt b)) = b
hasExt (SKEINKey _ (HasExt b) _) = b hasExt (SKEINKey _ (HasExt b)) = b
hasExt (Blake2bKey _ (HasExt b) _) = b hasExt (Blake2bKey _ (HasExt b)) = b
hasExt (Blake2sKey _ (HasExt b) _) = b hasExt (Blake2sKey _ (HasExt b)) = b
hasExt (Blake2spKey _ (HasExt b) _) = b hasExt (Blake2spKey _ (HasExt b)) = b
hasExt (SHA1Key (HasExt b) _) = b hasExt (SHA1Key (HasExt b)) = b
hasExt (MD5Key (HasExt b) _) = b hasExt (MD5Key (HasExt b)) = b
hasExt WORMKey = False hasExt WORMKey = False
hasExt URLKey = False hasExt URLKey = False
hasExt (OtherKey s) = (snd <$> S8.unsnoc s) == Just 'E' hasExt (OtherKey s) = (snd <$> S8.unsnoc s) == Just 'E'
sameExceptExt :: KeyVariety -> KeyVariety -> Bool sameExceptExt :: KeyVariety -> KeyVariety -> Bool
sameExceptExt (SHA2Key sz1 _ _) (SHA2Key sz2 _ _) = sz1 == sz2 sameExceptExt (SHA2Key sz1 _) (SHA2Key sz2 _) = sz1 == sz2
sameExceptExt (SHA3Key sz1 _ _) (SHA3Key sz2 _ _) = sz1 == sz2 sameExceptExt (SHA3Key sz1 _) (SHA3Key sz2 _) = sz1 == sz2
sameExceptExt (SKEINKey sz1 _ _) (SKEINKey sz2 _ _) = sz1 == sz2 sameExceptExt (SKEINKey sz1 _) (SKEINKey sz2 _) = sz1 == sz2
sameExceptExt (Blake2bKey sz1 _ _) (Blake2bKey sz2 _ _) = sz1 == sz2 sameExceptExt (Blake2bKey sz1 _) (Blake2bKey sz2 _) = sz1 == sz2
sameExceptExt (Blake2sKey sz1 _ _) (Blake2sKey sz2 _ _) = sz1 == sz2 sameExceptExt (Blake2sKey sz1 _) (Blake2sKey sz2 _) = sz1 == sz2
sameExceptExt (Blake2spKey sz1 _ _) (Blake2spKey sz2 _ _) = sz1 == sz2 sameExceptExt (Blake2spKey sz1 _) (Blake2spKey sz2 _) = sz1 == sz2
sameExceptExt (SHA1Key _ _) (SHA1Key _ _) = True sameExceptExt (SHA1Key _) (SHA1Key _) = True
sameExceptExt (MD5Key _ _) (MD5Key _ _) = True sameExceptExt (MD5Key _) (MD5Key _) = True
sameExceptExt _ _ = False sameExceptExt _ _ = False
{- Is the Key variety cryptographically secure, such that no two differing {- Is the Key variety cryptographically secure, such that no two differing
- file contents can be mapped to the same Key? -} - file contents can be mapped to the same Key? -}
cryptographicallySecure :: KeyVariety -> Bool cryptographicallySecure :: KeyVariety -> Bool
cryptographicallySecure (SHA2Key _ _ _) = True cryptographicallySecure (SHA2Key _ _) = True
cryptographicallySecure (SHA3Key _ _ _) = True cryptographicallySecure (SHA3Key _ _) = True
cryptographicallySecure (SKEINKey _ _ _) = True cryptographicallySecure (SKEINKey _ _) = True
cryptographicallySecure (Blake2bKey _ _ _) = True cryptographicallySecure (Blake2bKey _ _) = True
cryptographicallySecure (Blake2sKey _ _ _) = True cryptographicallySecure (Blake2sKey _ _) = True
cryptographicallySecure (Blake2spKey _ _ _) = True cryptographicallySecure (Blake2spKey _ _) = True
cryptographicallySecure _ = False cryptographicallySecure _ = False
{- Is the Key variety backed by a hash, which allows verifying content? {- Is the Key variety backed by a hash, which allows verifying content?
@ -98,86 +94,91 @@ cryptographicallySecure _ = False
- attacks. - attacks.
-} -}
isVerifiable :: KeyVariety -> Bool isVerifiable :: KeyVariety -> Bool
isVerifiable (SHA2Key _ _ _) = True isVerifiable (SHA2Key _ _) = True
isVerifiable (SHA3Key _ _ _) = True isVerifiable (SHA3Key _ _) = True
isVerifiable (SKEINKey _ _ _) = True isVerifiable (SKEINKey _ _) = True
isVerifiable (Blake2bKey _ _ _) = True isVerifiable (Blake2bKey _ _) = True
isVerifiable (Blake2sKey _ _ _) = True isVerifiable (Blake2sKey _ _) = True
isVerifiable (Blake2spKey _ _ _) = True isVerifiable (Blake2spKey _ _) = True
isVerifiable (SHA1Key _ _) = True isVerifiable (SHA1Key _) = True
isVerifiable (MD5Key _ _) = True isVerifiable (MD5Key _) = True
isVerifiable WORMKey = False isVerifiable WORMKey = False
isVerifiable URLKey = False isVerifiable URLKey = False
isVerifiable (OtherKey _) = False isVerifiable (OtherKey _) = False
formatKeyVariety :: KeyVariety -> S.ByteString formatKeyVariety :: KeyVariety -> S.ByteString
formatKeyVariety v = case v of formatKeyVariety v = case v of
SHA2Key sz e f -> f ! adde e (addsz sz "SHA") SHA2Key sz e -> adde e (addsz sz "SHA")
SHA3Key sz e f -> f ! adde e (addsz sz "SHA3_") SHA3Key sz e -> adde e (addsz sz "SHA3_")
SKEINKey sz e f -> f ! adde e (addsz sz "SKEIN") SKEINKey sz e -> adde e (addsz sz "SKEIN")
Blake2bKey sz e f -> f ! adde e (addsz sz "BLAKE2B") Blake2bKey sz e -> adde e (addsz sz "BLAKE2B")
Blake2sKey sz e f -> f ! adde e (addsz sz "BLAKE2S") Blake2sKey sz e -> adde e (addsz sz "BLAKE2S")
Blake2spKey sz e f -> f ! adde e (addsz sz "BLAKE2SP") Blake2spKey sz e -> adde e (addsz sz "BLAKE2SP")
SHA1Key e f -> f ! adde e "SHA1" SHA1Key e -> adde e "SHA1"
MD5Key e f -> f ! adde e "MD5" MD5Key e -> adde e "MD5"
WORMKey -> "WORM" WORMKey -> "WORM"
URLKey -> "URL" URLKey -> "URL"
OtherKey s -> s OtherKey s -> s
where where
adde (HasExt False) s = s adde (HasExt False) s = s
adde (HasExt True) s = s <> "E" adde (HasExt True) s = s <> "E"
addsz (HashSize n) s = s <> S8.pack (show n) addsz (HashSize n) s = s <> case n of
256 -> "256"
f ! s = if S.null f then s else f 512 -> "512"
224 -> "224"
384 -> "384"
160 -> "160"
-- This is relatively slow, which is why the common hash
-- sizes are hardcoded above.
_ -> S8.pack (show n)
parseKeyVariety :: S.ByteString -> KeyVariety parseKeyVariety :: S.ByteString -> KeyVariety
parseKeyVariety b parseKeyVariety "SHA256" = SHA2Key (HashSize 256) (HasExt False)
| b == "SHA256" = SHA2Key (HashSize 256) (HasExt False) b parseKeyVariety "SHA256E" = SHA2Key (HashSize 256) (HasExt True)
| b == "SHA256E" = SHA2Key (HashSize 256) (HasExt True) b parseKeyVariety "SHA512" = SHA2Key (HashSize 512) (HasExt False)
| b == "SHA512" = SHA2Key (HashSize 512) (HasExt False) b parseKeyVariety "SHA512E" = SHA2Key (HashSize 512) (HasExt True)
| b == "SHA512E" = SHA2Key (HashSize 512) (HasExt True) b parseKeyVariety "SHA224" = SHA2Key (HashSize 224) (HasExt False)
| b == "SHA224" = SHA2Key (HashSize 224) (HasExt False) b parseKeyVariety "SHA224E" = SHA2Key (HashSize 224) (HasExt True)
| b == "SHA224E" = SHA2Key (HashSize 224) (HasExt True) b parseKeyVariety "SHA384" = SHA2Key (HashSize 384) (HasExt False)
| b == "SHA384" = SHA2Key (HashSize 384) (HasExt False) b parseKeyVariety "SHA384E" = SHA2Key (HashSize 384) (HasExt True)
| b == "SHA384E" = SHA2Key (HashSize 384) (HasExt True) b parseKeyVariety "SHA3_512" = SHA3Key (HashSize 512) (HasExt False)
| b == "SHA3_512" = SHA3Key (HashSize 512) (HasExt False) b parseKeyVariety "SHA3_512E" = SHA3Key (HashSize 512) (HasExt True)
| b == "SHA3_512E" = SHA3Key (HashSize 512) (HasExt True) b parseKeyVariety "SHA3_384" = SHA3Key (HashSize 384) (HasExt False)
| b == "SHA3_384" = SHA3Key (HashSize 384) (HasExt False) b parseKeyVariety "SHA3_384E" = SHA3Key (HashSize 384) (HasExt True)
| b == "SHA3_384E" = SHA3Key (HashSize 384) (HasExt True) b parseKeyVariety "SHA3_256" = SHA3Key (HashSize 256) (HasExt False)
| b == "SHA3_256" = SHA3Key (HashSize 256) (HasExt False) b parseKeyVariety "SHA3_256E" = SHA3Key (HashSize 256) (HasExt True)
| b == "SHA3_256E" = SHA3Key (HashSize 256) (HasExt True) b parseKeyVariety "SHA3_224" = SHA3Key (HashSize 224) (HasExt False)
| b == "SHA3_224" = SHA3Key (HashSize 224) (HasExt False) b parseKeyVariety "SHA3_224E" = SHA3Key (HashSize 224) (HasExt True)
| b == "SHA3_224E" = SHA3Key (HashSize 224) (HasExt True) b parseKeyVariety "SKEIN512" = SKEINKey (HashSize 512) (HasExt False)
| b == "SKEIN512" = SKEINKey (HashSize 512) (HasExt False) b parseKeyVariety "SKEIN512E" = SKEINKey (HashSize 512) (HasExt True)
| b == "SKEIN512E" = SKEINKey (HashSize 512) (HasExt True) b parseKeyVariety "SKEIN256" = SKEINKey (HashSize 256) (HasExt False)
| b == "SKEIN256" = SKEINKey (HashSize 256) (HasExt False) b parseKeyVariety "SKEIN256E" = SKEINKey (HashSize 256) (HasExt True)
| b == "SKEIN256E" = SKEINKey (HashSize 256) (HasExt True) b
#if MIN_VERSION_cryptonite(0,23,0) #if MIN_VERSION_cryptonite(0,23,0)
| b == "BLAKE2B160" = Blake2bKey (HashSize 160) (HasExt False) b parseKeyVariety "BLAKE2B160" = Blake2bKey (HashSize 160) (HasExt False)
| b == "BLAKE2B160E" = Blake2bKey (HashSize 160) (HasExt True) b parseKeyVariety "BLAKE2B160E" = Blake2bKey (HashSize 160) (HasExt True)
| b == "BLAKE2B224" = Blake2bKey (HashSize 224) (HasExt False) b parseKeyVariety "BLAKE2B224" = Blake2bKey (HashSize 224) (HasExt False)
| b == "BLAKE2B224E" = Blake2bKey (HashSize 224) (HasExt True) b parseKeyVariety "BLAKE2B224E" = Blake2bKey (HashSize 224) (HasExt True)
| b == "BLAKE2B256" = Blake2bKey (HashSize 256) (HasExt False) b parseKeyVariety "BLAKE2B256" = Blake2bKey (HashSize 256) (HasExt False)
| b == "BLAKE2B256E" = Blake2bKey (HashSize 256) (HasExt True) b parseKeyVariety "BLAKE2B256E" = Blake2bKey (HashSize 256) (HasExt True)
| b == "BLAKE2B384" = Blake2bKey (HashSize 384) (HasExt False) b parseKeyVariety "BLAKE2B384" = Blake2bKey (HashSize 384) (HasExt False)
| b == "BLAKE2B384E" = Blake2bKey (HashSize 384) (HasExt True) b parseKeyVariety "BLAKE2B384E" = Blake2bKey (HashSize 384) (HasExt True)
| b == "BLAKE2B512" = Blake2bKey (HashSize 512) (HasExt False) b parseKeyVariety "BLAKE2B512" = Blake2bKey (HashSize 512) (HasExt False)
| b == "BLAKE2B512E" = Blake2bKey (HashSize 512) (HasExt True) b parseKeyVariety "BLAKE2B512E" = Blake2bKey (HashSize 512) (HasExt True)
| b == "BLAKE2S160" = Blake2sKey (HashSize 160) (HasExt False) b parseKeyVariety "BLAKE2S160" = Blake2sKey (HashSize 160) (HasExt False)
| b == "BLAKE2S160E" = Blake2sKey (HashSize 160) (HasExt True) b parseKeyVariety "BLAKE2S160E" = Blake2sKey (HashSize 160) (HasExt True)
| b == "BLAKE2S224" = Blake2sKey (HashSize 224) (HasExt False) b parseKeyVariety "BLAKE2S224" = Blake2sKey (HashSize 224) (HasExt False)
| b == "BLAKE2S224E" = Blake2sKey (HashSize 224) (HasExt True) b parseKeyVariety "BLAKE2S224E" = Blake2sKey (HashSize 224) (HasExt True)
| b == "BLAKE2S256" = Blake2sKey (HashSize 256) (HasExt False) b parseKeyVariety "BLAKE2S256" = Blake2sKey (HashSize 256) (HasExt False)
| b == "BLAKE2S256E" = Blake2sKey (HashSize 256) (HasExt True) b parseKeyVariety "BLAKE2S256E" = Blake2sKey (HashSize 256) (HasExt True)
| b == "BLAKE2SP224" = Blake2spKey (HashSize 224) (HasExt False) b parseKeyVariety "BLAKE2SP224" = Blake2spKey (HashSize 224) (HasExt False)
| b == "BLAKE2SP224E" = Blake2spKey (HashSize 224) (HasExt True) b parseKeyVariety "BLAKE2SP224E" = Blake2spKey (HashSize 224) (HasExt True)
| b == "BLAKE2SP256" = Blake2spKey (HashSize 256) (HasExt False) b parseKeyVariety "BLAKE2SP256" = Blake2spKey (HashSize 256) (HasExt False)
| b == "BLAKE2SP256E" = Blake2spKey (HashSize 256) (HasExt True) b parseKeyVariety "BLAKE2SP256E" = Blake2spKey (HashSize 256) (HasExt True)
#endif #endif
| b == "SHA1" = SHA1Key (HasExt False) b parseKeyVariety "SHA1" = SHA1Key (HasExt False)
| b == "SHA1E" = SHA1Key (HasExt True) b parseKeyVariety "SHA1E" = SHA1Key (HasExt True)
| b == "MD5" = MD5Key (HasExt False) b parseKeyVariety "MD5" = MD5Key (HasExt False)
| b == "MD5E" = MD5Key (HasExt True) b parseKeyVariety "MD5E" = MD5Key (HasExt True)
| b == "WORM" = WORMKey parseKeyVariety "WORM" = WORMKey
| b == "URL" = URLKey parseKeyVariety "URL" = URLKey
| otherwise = OtherKey b parseKeyVariety b = OtherKey b