From ffce7d3139b2c9f74fb1a1e35e9be4b99d6c3cdf Mon Sep 17 00:00:00 2001 From: Sashwat K Date: Mon, 29 Apr 2024 15:08:46 +0530 Subject: [PATCH] feat: add support to generate checksum for HpcrText and HpcrJson --- contract/contract.go | 16 ++++++++-------- contract/contract_test.go | 32 +++++++++++++++++++------------- 2 files changed, 27 insertions(+), 21 deletions(-) diff --git a/contract/contract.go b/contract/contract.go index 6a07626..27c6a94 100644 --- a/contract/contract.go +++ b/contract/contract.go @@ -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 diff --git a/contract/contract_test.go b/contract/contract_test.go index 990a868..26e64d4 100644 --- a/contract/contract_test.go +++ b/contract/contract_test.go @@ -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) } @@ -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) } @@ -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) }