Skip to content

Commit

Permalink
Feature/xc 1539 (#21)
Browse files Browse the repository at this point in the history
* add hash256 func

* add hash hasStr function

---------

Co-authored-by: tbal999 <[email protected]>
  • Loading branch information
tbal999 and tbal999 authored Nov 9, 2023
1 parent 0a4fc93 commit bf6cd95
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
7 changes: 6 additions & 1 deletion env.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/xiatechs/jsonata-go/jlib"
"github.com/xiatechs/jsonata-go/jparse"
"github.com/xiatechs/jsonata-go/jtypes"

)

type environment struct {
Expand Down Expand Up @@ -95,6 +94,12 @@ var baseEnv = initBaseEnv(map[string]Extension{
EvalContextHandler: defaultContextHandler,
},

"hashStr": {
Func: jlib.Hash,
UndefinedHandler: defaultUndefinedHandler,
EvalContextHandler: defaultContextHandler,
},

"objectsToDocument": {
Func: jlib.ObjectsToDocument,
UndefinedHandler: defaultUndefinedHandler,
Expand Down
37 changes: 37 additions & 0 deletions jlib/hash.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package jlib

import (
"crypto/md5"
"crypto/sha256"
"encoding/hex"
"strings"
)

// Hash a input string into a sha256 string
// for deduplication purposes
func Hash(hashType, input string) string {
switch strings.ToLower(hashType) {
case "sha256":
hasher := sha256.New()

hasher.Write([]byte(input))

hashedBytes := hasher.Sum(nil)

hashedString := hex.EncodeToString(hashedBytes)

return hashedString
case "md5":
hash := md5.Sum([]byte(input))

hashedString := hex.EncodeToString(hash[:])

return hashedString
}

hash := md5.Sum([]byte(input))

hashedString := hex.EncodeToString(hash[:])

return hashedString
}

0 comments on commit bf6cd95

Please sign in to comment.