Hashing credit card number

Report a typo

Suppose you have a Go program that takes as input a string with a credit card number and saves it to the cardNumber variable.

Your task is to create a function hashCreditCard() that takes as an argument a string cardNumber, uses a switch statement to apply a different hashing algorithm based on the first number of the credit card, and finally returns the hashed cardNumber.

  • If the cardNumber starts with the number 3, you should apply SHA512 to it once;

  • If the cardNumber starts with the number 4, you should apply SHA256 to it iteratively 4 times;

  • If the cardNumber starts with the number 5, you should apply MD5 to it iteratively 5 times.

Note that you can use the strings.HasPrefix() function to easily check if the credit card number starts with a 3, 4 or 5.

Sample Input 1:

374245455400126

Sample Output 1:

6f0b3c02093698ab4c910c2a70f69cc2da4ba21e581fd3278b131d79a66f763c2ff9f2ec55fba108e8bd65888e4779739cbe9b007ee953c2d6f57e31ab995ebc

Sample Input 2:

4263982640269299

Sample Output 2:

a0baf4f2e333e7ec2d4c876fd211a4c8a900dacd023a89af05e4055d45e4d0f2

Sample Input 3:

5425233430109903

Sample Output 3:

2b8b91df4ee01b41820f8dd4a79a5008
Write a program in Go
package main

import (
"crypto/md5"
"crypto/sha256"
"crypto/sha512"
"fmt"
"strings"
)

func hashCreditCard(cardNumber string) string {
switch {
case strings.HasPrefix(cardNumber, "3"):
sha512Hash := sha512.New()
sha512Hash.?
cardNumber = fmt.Sprintf("%x", sha512Hash.Sum(nil))

case strings.HasPrefix(?, "4"):
for i := 0; i < ?; i++ {
? // HINT: use SHA256 here!
}

case strings.HasPrefix(cardNumber, "?"):
for i := 0; i < 5; i++ {
? // HINT: use MD5 here!
}
}
return cardNumber
}

// DO NOT delete or modify the contents of the main() function!
func main() {
var creditCardNumber string
fmt.Scanln(&creditCardNumber)

fmt.Println(hashCreditCard(creditCardNumber))
___

Create a free account to access the full topic