Skip to content

Commit

Permalink
feat(logic): add SHA-512 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 8177f62 commit 93b2d59
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 17 deletions.
28 changes: 14 additions & 14 deletions x/logic/predicate/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func SHAHash(vm *engine.VM, data, hash engine.Term, cont engine.Cont, env *engin
// For Alg, the supported algorithms are:
//
// - sha256 (default): The SHA-256 algorithm.
// - sha512: The SHA-512 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 All @@ -93,29 +94,28 @@ func CryptoDataHash(
algorithmOpt := engine.NewAtom("algorithm")

return engine.Delay(func(ctx context.Context) *engine.Promise {
algorithm, err := getOptionAsAtomWithDefault(algorithmOpt, options, engine.NewAtom("sha256"), env, functor)
algorithmAtom, err := getOptionAsAtomWithDefault(algorithmOpt, options, engine.NewAtom("sha256"), env, functor)
if err != nil {
return engine.Error(err)
}
algorithm, err := util.ParseHashAlg(algorithmAtom.String())
if err != nil {
return engine.Error(fmt.Errorf("%s: invalid algorithm: %s. Possible values: %s",
functor,
algorithmAtom.String(),
util.HashAlgNames()))
}
decodedData, err := TermToBytes(data, options, AtomUtf8, env)
if err != nil {
return engine.Error(fmt.Errorf("%s: failed to decode data: %w", functor, err))
}

switch algorithm.String() {
case util.HashAlgSha256.String():
result, err := util.Hash(util.HashAlgSha256, decodedData)
if err != nil {
engine.Error(fmt.Errorf("sha_hash/2: failed to hash data: %w", err))
}

return engine.Unify(vm, hash, BytesToList(result), cont, env)
default:
return engine.Error(fmt.Errorf("%s: invalid algorithm: %s. Possible values: %s",
functor,
algorithm.String(),
util.HashAlgNames()))
result, err := util.Hash(algorithm, decodedData)
if err != nil {
engine.Error(fmt.Errorf("sha_hash/2: failed to hash data: %w", err))
}

return engine.Unify(vm, hash, BytesToList(result), cont, env)
})
}

Expand Down
7 changes: 6 additions & 1 deletion x/logic/util/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"crypto/ed25519"
"crypto/elliptic"
"crypto/sha256"
"crypto/sha512"
"fmt"

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

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

// VerifySignature verifies the signature of the given message with the given public key using the given algorithm.
Expand Down Expand Up @@ -56,6 +57,10 @@ func Hash(alg HashAlg, bytes []byte) ([]byte, error) {
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)
}
Expand Down
9 changes: 7 additions & 2 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 93b2d59

Please sign in to comment.