Skip to content

Commit

Permalink
feat: add support to return checksum for HpcrEncryptedtext and HpcrEn…
Browse files Browse the repository at this point in the history
…cryptedJson
  • Loading branch information
Sashwat-K committed Apr 29, 2024
1 parent ffce7d3 commit 0b5015a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
17 changes: 10 additions & 7 deletions contract/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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()
Expand All @@ -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
}
14 changes: 9 additions & 5 deletions contract/contract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}

0 comments on commit 0b5015a

Please sign in to comment.