Skip to content

Commit

Permalink
feat(logic): add MD5 algorithm to crypto_data_hash/3
Browse files Browse the repository at this point in the history
  • Loading branch information
ccamel committed Nov 7, 2023
1 parent 46f10c8 commit 7e17001
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 21 deletions.
2 changes: 1 addition & 1 deletion x/logic/interpreter/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ var registry = map[string]any{
"bank_spendable_balances/2": predicate.BankSpendableBalances,
"bank_locked_balances/2": predicate.BankLockedBalances,
"did_components/2": predicate.DIDComponents,
"crypto_data_hash": predicate.CryptoDataHash,
"crypto_data_hash/3": predicate.CryptoDataHash,
"hex_bytes/2": predicate.HexBytes,
"bech32_address/2": predicate.Bech32Address,
"source_file/1": predicate.SourceFile,
Expand Down
1 change: 1 addition & 0 deletions x/logic/predicate/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ func SHAHash(vm *engine.VM, data, hash engine.Term, cont engine.Cont, env *engin
//
// - sha256 (default): The SHA-256 algorithm.
// - sha512: The SHA-512 algorithm.
// - md5: (insecure) The MD5 algorithm.
//
// Note: Due to the principles of the hash algorithm (pre-image resistance), this predicate can only compute the hash
// value from input data, and cannot compute the original input data from the hash value.
Expand Down
35 changes: 23 additions & 12 deletions x/logic/util/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import (
"crypto/ecdsa"
"crypto/ed25519"
"crypto/elliptic"
"crypto/md5"
"crypto/sha256"
"crypto/sha512"
"fmt"
"hash"

"github.com/dustinxie/ecc"
)
Expand All @@ -27,9 +29,23 @@ func (a KeyAlg) String() string {
}

// HashAlg is the type of hash algorithm supported by the crypto util functions.
// ENUM(sha256,sha512)
// ENUM(md5,sha256,sha512)
type HashAlg int

// Hasher returns a new hash.Hash for the given algorithm.
func (a HashAlg) Hasher() (hash.Hash, error) {
switch a {
case HashAlgMd5:
return md5.New(), nil
case HashAlgSha256:
return sha256.New(), nil
case HashAlgSha512:
return sha512.New(), nil
default:
return nil, fmt.Errorf("algo %s not supported", a.String())
}
}

// VerifySignature verifies the signature of the given message with the given public key using the given algorithm.
func VerifySignature(alg KeyAlg, pubKey []byte, msg, sig []byte) (_ bool, err error) {
defer func() {
Expand All @@ -52,18 +68,13 @@ func VerifySignature(alg KeyAlg, pubKey []byte, msg, sig []byte) (_ bool, err er

// Hash hashes the given data using the given algorithm.
func Hash(alg HashAlg, bytes []byte) ([]byte, error) {
switch alg {
case HashAlgSha256:
hasher := sha256.New()
hasher.Write(bytes)
return hasher.Sum(nil), nil
case HashAlgSha512:
hasher := sha512.New()
hasher.Write(bytes)
return hasher.Sum(nil), nil
default:
return nil, fmt.Errorf("algo %s not supported", alg)
hasher, err := alg.Hasher()
if err != nil {
return nil, err
}

hasher.Write(bytes)
return hasher.Sum(nil), nil
}

// verifySignatureWithCurve verifies the ASN1 signature of the given message with the given
Expand Down
21 changes: 13 additions & 8 deletions x/logic/util/crypto_enum.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7e17001

Please sign in to comment.