From 4c579ecaa86282b13517c30e7e5cca38782a22f6 Mon Sep 17 00:00:00 2001 From: Evis Drenova Date: Tue, 14 Nov 2023 15:38:55 -0800 Subject: [PATCH] updated luhn check --- .../benthos/transformers/card_number.go | 22 +++++++++++-------- .../benthos/transformers/card_number_test.go | 2 +- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/worker/internal/benthos/transformers/card_number.go b/worker/internal/benthos/transformers/card_number.go index 808306fd30..4bb0ddac44 100644 --- a/worker/internal/benthos/transformers/card_number.go +++ b/worker/internal/benthos/transformers/card_number.go @@ -43,7 +43,7 @@ func GenerateCardNumber(luhn bool) (int64, error) { if luhn { - val, err := GenerateValidVLuhnCheckCardNumber() + val, err := GenerateValidLuhnCheckCardNumber() if err != nil { return 0, fmt.Errorf("unable to generate a luhn valid card number") @@ -67,7 +67,7 @@ func GenerateCardNumber(luhn bool) (int64, error) { } // generates a card number that passes luhn validation -func GenerateValidVLuhnCheckCardNumber() (int64, error) { +func GenerateValidLuhnCheckCardNumber() (int64, error) { // To find the checksum digit on cardNo := make([]int, 0) @@ -82,15 +82,19 @@ func GenerateValidVLuhnCheckCardNumber() (int64, error) { } // Acc no (9 digits) - nineDigits, _ := transformer_utils.GenerateRandomInt(int64(9)) + nineDigits, err := transformer_utils.GenerateRandomInt(int64(9)) + if err != nil { + return 0, err + } strNine := strconv.FormatInt(nineDigits, 10) - var nineInts []int for _, k := range strNine { - nineInts = append(nineInts, int(k)) - } - for _, i := range nineInts { - cardNo = append(cardNo, i) - cardNum = append(cardNum, i) + digit, err := strconv.Atoi(string(k)) + if err != nil { + fmt.Println("Error converting to int:", err) + continue + } + cardNo = append(cardNo, digit) + cardNum = append(cardNum, digit) } // odd position digits diff --git a/worker/internal/benthos/transformers/card_number_test.go b/worker/internal/benthos/transformers/card_number_test.go index 932383e115..cb406c8897 100644 --- a/worker/internal/benthos/transformers/card_number_test.go +++ b/worker/internal/benthos/transformers/card_number_test.go @@ -10,7 +10,7 @@ import ( func TestGenerateValidLuhnCardNumber(t *testing.T) { - val, err := GenerateValidVLuhnCheckCardNumber() + val, err := GenerateValidLuhnCheckCardNumber() assert.NoError(t, err) assert.Len(t, strconv.FormatInt(val, 10), 16, "The output card should be 16 characters long")