Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SHA256 support #65

Merged
merged 1 commit into from
Feb 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/Crypto/Saltine/Core/Hash.hs
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,28 @@
-- This is version 2010.08.30 of the hash.html web page. Information
-- about SipHash has been added.
module Crypto.Saltine.Core.Hash (
-- * Documentation
ShorthashKey,
hash,
shorthash, newShorthashKey,
GenerichashKey,
newGenerichashKey,
GenerichashOutLen,
generichashOutLen, generichash
generichashOutLen, generichash,

-- * Advanced: SHA-2
--
-- | The SHA-256 function is provided for interoperability with
-- other applications. If you are looking for a generic hash
-- function and not specifically SHA-2, using 'generichash'
-- (BLAKE2b) might be a better choice.
sha256
) where

import Crypto.Saltine.Internal.Hash
( c_hash
, c_generichash
, c_hash_sha256
, shorthash
, ShorthashKey(..)
, GenerichashKey(..)
Expand All @@ -72,7 +82,7 @@
-> ByteString
-- ^ Hash
hash m = snd . buildUnsafeByteString Bytes.hash_bytes $ \ph ->
constByteStrings [m] $ \[(pm, _)] -> c_hash ph pm (fromIntegral $ S.length m)

Check warning on line 85 in src/Crypto/Saltine/Core/Hash.hs

View workflow job for this annotation

GitHub Actions / build (9.4.4)

Pattern match(es) are non-exhaustive

Check warning on line 85 in src/Crypto/Saltine/Core/Hash.hs

View workflow job for this annotation

GitHub Actions / build (9.2.6)

Pattern match(es) are non-exhaustive

-- | Randomly generates a new key for 'shorthash'.
newShorthashKey :: IO ShorthashKey
Expand Down Expand Up @@ -101,3 +111,8 @@
generichash (GhK k) m (GhOL outLen) = snd . buildUnsafeByteString outLen $ \ph ->
constByteStrings [k, m] $ \[(pk, _), (pm, _)] ->
c_generichash ph (fromIntegral outLen) pm (fromIntegral $ S.length m) pk (fromIntegral $ S.length k)

-- | Computes a SHA256 hash.
sha256 :: ByteString -> ByteString
sha256 m = snd . buildUnsafeByteString Bytes.hash_sha256_bytes $ \ph ->
constByteStrings [m] $ \[(pm,_)] -> c_hash_sha256 ph pm (fromIntegral $ S.length m)
22 changes: 21 additions & 1 deletion src/Crypto/Saltine/Internal/Hash.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
, shorthash_keybytes
, generichash_bytes_max
, generichash_keybytes_max
, hash_sha256_bytes
, c_hash
, c_shorthash
, c_generichash
, c_hash_sha256
, nullShKey
, shorthash
, ShorthashKey(..)
Expand All @@ -30,7 +32,7 @@
import Data.ByteString (ByteString)
import Data.Data (Data, Typeable)
import Data.Hashable (Hashable)
import Data.Monoid

Check warning on line 35 in src/Crypto/Saltine/Internal/Hash.hs

View workflow job for this annotation

GitHub Actions / build (8.4.4)

The import of ‘Data.Monoid’ is redundant

Check warning on line 35 in src/Crypto/Saltine/Internal/Hash.hs

View workflow job for this annotation

GitHub Actions / build (8.4.4)

The import of ‘Data.Monoid’ is redundant

Check warning on line 35 in src/Crypto/Saltine/Internal/Hash.hs

View workflow job for this annotation

GitHub Actions / build (8.6.5)

The import of ‘Data.Monoid’ is redundant

Check warning on line 35 in src/Crypto/Saltine/Internal/Hash.hs

View workflow job for this annotation

GitHub Actions / build (8.6.5)

The import of ‘Data.Monoid’ is redundant

Check warning on line 35 in src/Crypto/Saltine/Internal/Hash.hs

View workflow job for this annotation

GitHub Actions / build (9.4.4)

The import of ‘Data.Monoid’ is redundant

Check warning on line 35 in src/Crypto/Saltine/Internal/Hash.hs

View workflow job for this annotation

GitHub Actions / build (9.4.4)

The import of ‘Data.Monoid’ is redundant

Check warning on line 35 in src/Crypto/Saltine/Internal/Hash.hs

View workflow job for this annotation

GitHub Actions / build (8.8.4)

The import of ‘Data.Monoid’ is redundant

Check warning on line 35 in src/Crypto/Saltine/Internal/Hash.hs

View workflow job for this annotation

GitHub Actions / build (8.8.4)

The import of ‘Data.Monoid’ is redundant

Check warning on line 35 in src/Crypto/Saltine/Internal/Hash.hs

View workflow job for this annotation

GitHub Actions / build (9.2.6)

The import of ‘Data.Monoid’ is redundant

Check warning on line 35 in src/Crypto/Saltine/Internal/Hash.hs

View workflow job for this annotation

GitHub Actions / build (9.2.6)

The import of ‘Data.Monoid’ is redundant

Check warning on line 35 in src/Crypto/Saltine/Internal/Hash.hs

View workflow job for this annotation

GitHub Actions / build (8.10.7)

The import of ‘Data.Monoid’ is redundant

Check warning on line 35 in src/Crypto/Saltine/Internal/Hash.hs

View workflow job for this annotation

GitHub Actions / build (8.10.7)

The import of ‘Data.Monoid’ is redundant

Check warning on line 35 in src/Crypto/Saltine/Internal/Hash.hs

View workflow job for this annotation

GitHub Actions / build (9.0.2)

The import of ‘Data.Monoid’ is redundant

Check warning on line 35 in src/Crypto/Saltine/Internal/Hash.hs

View workflow job for this annotation

GitHub Actions / build (9.0.2)

The import of ‘Data.Monoid’ is redundant
import Foreign.C
import Foreign.Ptr
import GHC.Generics (Generic)
Expand All @@ -57,7 +59,7 @@
-> ByteString
-- ^ Hash
shorthash (ShK k) m = snd . buildUnsafeByteString shorthash_bytes $ \ph ->
constByteStrings [k, m] $ \[(pk, _), (pm, _)] ->

Check warning on line 62 in src/Crypto/Saltine/Internal/Hash.hs

View workflow job for this annotation

GitHub Actions / build (9.4.4)

Pattern match(es) are non-exhaustive

Check warning on line 62 in src/Crypto/Saltine/Internal/Hash.hs

View workflow job for this annotation

GitHub Actions / build (9.4.4)

Pattern match(es) are non-exhaustive

Check warning on line 62 in src/Crypto/Saltine/Internal/Hash.hs

View workflow job for this annotation

GitHub Actions / build (9.2.6)

Pattern match(es) are non-exhaustive

Check warning on line 62 in src/Crypto/Saltine/Internal/Hash.hs

View workflow job for this annotation

GitHub Actions / build (9.2.6)

Pattern match(es) are non-exhaustive
c_shorthash ph pm (fromIntegral $ S.length m) pk

instance IsEncoding ShorthashKey where
Expand Down Expand Up @@ -85,7 +87,7 @@

newtype GenerichashOutLen = GhOL { unGhOL :: Int } deriving (Eq, Ord, Hashable, Data, Typeable, Generic, NFData)

hash_bytes, shorthash_bytes, shorthash_keybytes, generichash_bytes_max, generichash_keybytes_max :: Int
hash_bytes, shorthash_bytes, shorthash_keybytes, generichash_bytes_max, generichash_keybytes_max, hash_sha256_bytes :: Int

-- Hashes
-- | The size of a hash resulting from
Expand All @@ -103,6 +105,9 @@
-- | The maximum key size of the generic hash function
-- 'Crypto.Saltine.Core.Hash.generichash'
generichash_keybytes_max = fromIntegral c_crypto_generichash_keybytes_max
-- | The size of a hash resulting from
-- 'Crypto.Saltine.Core.Hash.sha256'.
hash_sha256_bytes = fromIntegral c_crypto_hash_sha256_bytes

-- src/libsodium/crypto_generichash/crypto_generichash.c
foreign import ccall "crypto_generichash_bytes_max"
Expand All @@ -122,6 +127,11 @@
foreign import ccall "crypto_shorthash_keybytes"
c_crypto_shorthash_keybytes :: CSize

-- src/libsodium/crypto_hash/sha256/hash_sha256.c
-- src/libsodium/include/sodium/crypto_hash_sha256.h
foreign import ccall "crypto_hash_sha256_bytes"
c_crypto_hash_sha256_bytes :: CSize


foreign import ccall "crypto_hash"
c_hash :: Ptr CChar
Expand Down Expand Up @@ -160,3 +170,13 @@
-- ^ Key buffer length
-> IO CInt
-- ^ Always 0

foreign import ccall "crypto_hash_sha256"
c_hash_sha256 :: Ptr CChar
-- ^ Output hash buffer
-> Ptr CChar
-- ^ Constant message buffer
-> CULLong
-- ^ Message buffer length
-> IO CInt
-- ^ Always 0
Loading