From 0b5015ab9e347acb59ee71be5fd08e01be4ebb27 Mon Sep 17 00:00:00 2001 From: Sashwat K Date: Mon, 29 Apr 2024 15:27:07 +0530 Subject: [PATCH] feat: add support to return checksum for HpcrEncryptedtext and HpcrEncryptedJson --- contract/contract.go | 17 ++++++++++------- contract/contract_test.go | 14 +++++++++----- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/contract/contract.go b/contract/contract.go index 27c6a94..d56c075 100644 --- a/contract/contract.go +++ b/contract/contract.go @@ -23,15 +23,18 @@ func HpcrJson(plainJson string) (string, string, error) { return gen.EncodeToBase64(plainJson), gen.GenerateSha256(plainJson), nil } -// HpcrEncryptedtext - function to generate encrypted Hyper protect data from plain text -func HpcrEncryptedtext(plainText, encryptionCertificate string) (string, error) { +// HpcrEncryptedtext - function to generate encrypted Hyper protect data and SHA256 from plain text +func HpcrEncryptedtext(plainText, encryptionCertificate string) (string, string, error) { + if plainText == "" { + return "", "", fmt.Errorf("input text is empty") + } return Encrypter(plainText, encryptionCertificate) } -// HpcrEncryptedJson - function to generate encrypted hyper protect data from plain JSON data -func HpcrEncryptedJson(plainJson, encryptionCertificate string) (string, error) { +// HpcrEncryptedJson - function to generate encrypted hyper protect data and SHA256 from plain JSON data +func HpcrEncryptedJson(plainJson, encryptionCertificate string) (string, string, error) { if !gen.IsJSON(plainJson) { - return "", fmt.Errorf("contract is not a JSON data") + return "", "", fmt.Errorf("contract is not a JSON data") } return Encrypter(plainJson, encryptionCertificate) } @@ -56,7 +59,7 @@ func HpcrTgz(folderPath string) (string, error) { } // Encrypter - function to generate encrypted hyper protect data from plain string -func Encrypter(stringText, encryptionCertificate string) (string, error) { +func Encrypter(stringText, encryptionCertificate string) (string, string, error) { encCert := gen.FetchEncryptionCertificate(encryptionCertificate) password, err := enc.RandomPasswordGenerator() @@ -74,5 +77,5 @@ func Encrypter(stringText, encryptionCertificate string) (string, error) { fmt.Println(err) } - return enc.EncryptFinalStr(encodedEncryptedPassowrd, encryptedString), nil + return enc.EncryptFinalStr(encodedEncryptedPassowrd, encryptedString), gen.GenerateSha256(stringText), nil } diff --git a/contract/contract_test.go b/contract/contract_test.go index 26e64d4..f5de310 100644 --- a/contract/contract_test.go +++ b/contract/contract_test.go @@ -45,24 +45,26 @@ func TestHpcrJson(t *testing.T) { assert.Equal(t, sha256, sampleChecksumJson) } -// Testcase to check if TestHpcrEncryptedtext() is able to encrypt text +// Testcase to check if TestHpcrEncryptedtext() is able to encrypt text and generate SHA256 func TestHpcrEncryptedtext(t *testing.T) { - result, err := HpcrEncryptedtext(sampleStringData, "") + result, sha256, err := HpcrEncryptedtext(sampleStringData, "") if err != nil { fmt.Println(err) } assert.Contains(t, result, "hyper-protect-basic.") + assert.Equal(t, sha256, sampleDataChecksum) } -// Testcase to check if TestHpcrEncryptedJson() is able to encrypt JSON +// Testcase to check if TestHpcrEncryptedJson() is able to encrypt JSON and generate SHA256 func TestHpcrEncryptedJson(t *testing.T) { - result, err := HpcrEncryptedJson(sampleStringJson, "") + result, sha256, err := HpcrEncryptedJson(sampleStringJson, "") if err != nil { fmt.Println(err) } assert.Contains(t, result, "hyper-protect-basic.") + assert.Equal(t, sha256, sampleChecksumJson) } // Testcase to check if HpcrTgz() is able to generate base64 of tar.tgz @@ -76,11 +78,13 @@ func TestHpcrTgz(t *testing.T) { assert.NoError(t, err) } +// Testcase to check if Encrypter() is able to encrypt and generate SHA256 from string func TestEncrypter(t *testing.T) { - result, err := Encrypter(sampleStringJson, "") + result, sha256, err := Encrypter(sampleStringJson, "") if err != nil { fmt.Println(err) } assert.Contains(t, result, "hyper-protect-basic.") + assert.Equal(t, sha256, sampleChecksumJson) }