Skip to content

Commit

Permalink
feat: add support to generate checksum for HpcrText and HpcrJson
Browse files Browse the repository at this point in the history
  • Loading branch information
Sashwat-K committed Apr 29, 2024
1 parent abe9f43 commit ffce7d3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 21 deletions.
16 changes: 8 additions & 8 deletions contract/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@ import (
gen "github.com/Sashwat-K/lib-hpcr/common/general"
)

// HpcrText - function to generate base64 data from string
func HpcrText(plainText string) (string, error) {
// HpcrText - function to generate base64 data and checksum from string
func HpcrText(plainText string) (string, string, error) {
if plainText == "" {
return "", fmt.Errorf("input is empty")
return "", "", fmt.Errorf("input is empty")
}
return gen.EncodeToBase64(plainText), nil
return gen.EncodeToBase64(plainText), gen.GenerateSha256(plainText), nil
}

// HpcrJson - function to generate base64 data from JSON string
func HpcrJson(plainJson string) (string, error) {
// HpcrJson - function to generate base64 data and checksum from JSON string
func HpcrJson(plainJson string) (string, string, error) {
if !gen.IsJSON(plainJson) {
return "", fmt.Errorf("not a JSON data")
return "", "", fmt.Errorf("not a JSON data")
}
return gen.EncodeToBase64(plainJson), nil
return gen.EncodeToBase64(plainJson), gen.GenerateSha256(plainJson), nil
}

// HpcrEncryptedtext - function to generate encrypted Hyper protect data from plain text
Expand Down
32 changes: 19 additions & 13 deletions contract/contract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,46 @@ import (
)

const (
sampleBase64Data = "c2FzaHdhdGs="
sampleDecodedBase64Data = "sashwatk"
sampleBase64Json = "Cgl7CgkJInR5cGUiOiAiZW52IgoJfQoJ"
sampleDecodedBase64Json = `
sampleStringData = "sashwatk"
sampleBase64Data = "c2FzaHdhdGs="
sampleDataChecksum = "05fb716cba07a0cdda231f1aa19621ce9e183a4fb6e650b459bc3c5db7593e42"

sampleStringJson = `
{
"type": "env"
}
`
sampleBase64Json = "Cgl7CgkJInR5cGUiOiAiZW52IgoJfQoJ"
sampleChecksumJson = "f932f8ad556280f232f4b42d55b24ce7d2e909d3195ef60d49e92d49b735de2b"

sampleComposeFolderPath = "../samples/tgz"
)

// Testcase to check if TestHpcrText() is able to encode text
// Testcase to check if TestHpcrText() is able to encode text and generate SHA256
func TestHpcrText(t *testing.T) {
result, err := HpcrText(sampleDecodedBase64Data)
base64, sha256, err := HpcrText(sampleStringData)
if err != nil {
fmt.Println(err)
}

assert.Equal(t, result, sampleBase64Data)
assert.Equal(t, base64, sampleBase64Data)
assert.Equal(t, sha256, sampleDataChecksum)
}

// Testcase to check if HpcrJson() is able to encode JSON
// Testcase to check if HpcrJson() is able to encode JSON and generate SHA256
func TestHpcrJson(t *testing.T) {
result, err := HpcrJson(sampleDecodedBase64Json)
base64, sha256, err := HpcrJson(sampleStringJson)
if err != nil {
fmt.Println(err)
}

assert.Equal(t, result, sampleBase64Json)
assert.Equal(t, base64, sampleBase64Json)
assert.Equal(t, sha256, sampleChecksumJson)
}

// Testcase to check if TestHpcrEncryptedtext() is able to encrypt text
func TestHpcrEncryptedtext(t *testing.T) {
result, err := HpcrEncryptedtext(sampleDecodedBase64Data, "")
result, err := HpcrEncryptedtext(sampleStringData, "")
if err != nil {
fmt.Println(err)
}
Expand All @@ -51,7 +57,7 @@ func TestHpcrEncryptedtext(t *testing.T) {

// Testcase to check if TestHpcrEncryptedJson() is able to encrypt JSON
func TestHpcrEncryptedJson(t *testing.T) {
result, err := HpcrEncryptedJson(sampleDecodedBase64Json, "")
result, err := HpcrEncryptedJson(sampleStringJson, "")
if err != nil {
fmt.Println(err)
}
Expand All @@ -71,7 +77,7 @@ func TestHpcrTgz(t *testing.T) {
}

func TestEncrypter(t *testing.T) {
result, err := Encrypter(sampleDecodedBase64Json, "")
result, err := Encrypter(sampleStringJson, "")
if err != nil {
fmt.Println(err)
}
Expand Down

0 comments on commit ffce7d3

Please sign in to comment.