-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4f31fd7
commit a8f9c64
Showing
9 changed files
with
146 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package neosync_transformers | ||
|
||
import ( | ||
"bytes" | ||
"crypto/sha256" | ||
"encoding/hex" | ||
"fmt" | ||
|
||
"github.com/benthosdev/benthos/v4/public/bloblang" | ||
_ "github.com/benthosdev/benthos/v4/public/components/io" | ||
transformer_utils "github.com/nucleuscloud/neosync/worker/internal/benthos/transformers/utils" | ||
) | ||
|
||
func init() { | ||
|
||
spec := bloblang.NewPluginSpec() | ||
|
||
err := bloblang.RegisterFunctionV2("sha256hash", spec, func(args *bloblang.ParsedParams) (bloblang.Function, error) { | ||
|
||
return func() (any, error) { | ||
|
||
val, err := GenerateRandomSHA256Hash() | ||
|
||
if err != nil { | ||
return false, fmt.Errorf("unable to generate sha256 hash") | ||
} | ||
return val, nil | ||
}, nil | ||
}) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
func GenerateRandomSHA256Hash() (string, error) { | ||
|
||
length, err := transformer_utils.GenerateRandomInt(1) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
str, err := transformer_utils.GenerateRandomStringWithLength(length) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
// hash the value | ||
bites := []byte(str) | ||
hasher := sha256.New() | ||
_, err = hasher.Write(bites) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
// compute sha256 checksum and encode it into a hex string | ||
hashed := hasher.Sum(nil) | ||
var buf bytes.Buffer | ||
e := hex.NewEncoder(&buf) | ||
_, err = e.Write(hashed) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return buf.String(), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package neosync_transformers | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_GenerateSHA256Hash(t *testing.T) { | ||
|
||
res, err := GenerateRandomSHA256Hash() | ||
assert.NoError(t, err) | ||
|
||
assert.IsType(t, "", res) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters