Hashing a dollar bill serial number

Report a typo

As you know, most bills or banknotes have serial numbers. Below you can see part of a 5-dollar USA bill with the serial number IE25507922C:

banknotes serial number

Your task is to create a function hashSerialNumber() that takes as an argument a string serialNumber, then it should do the following:

  • If the serial number does not contain the E character (count == 0) or has an odd number of E characters ( count%2 != 0 ), apply SHA256 hashing to the serialNumber once.

  • If the number of E characters in the serial is even (count%2 == 0), use a for loop to iteratively apply MD5 hashing based on the total count of E characters in the serialNumber. For example, if the serialNumber has four E characters, you must apply MD5 hashing iteratively 4 (four) times.

Sample Input 1:

IE25507922C

Sample Output 1:

eb7a1a0666d5551ffd9a6b1544388b297122217b0469aca783c2246c667ca9c7

Sample Input 2:

JB00000000T

Sample Output 2:

a00bd4b8e63ba5e71e636ece6b8fda4de56f64d618c2635d4ed71f4a8bc89859

Sample Input 3:

E2E2222E21E

Sample Output 3:

dca5dc9360cfd3ea0236f6510d5042e5
Write a program in Go
package main

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

func hashSerialNumber(serialNumber string) string {
// The `count` variable stores the total number of 'E' characters in `serialNumber`:
count := strings.Count(serialNumber, "E")

// If the count of 'E' is 0 or odd, then apply SHA256 to `serialNumber` ONCE:
if count == 0 || count%2 != 0 {
sha256Hash := ?
sha256Hash.?

serialNumber = fmt.Sprintf("%x", sha256Hash.Sum(nil)) // DO NOT delete!
}

// If the count of 'E' is even, then apply MD5 to `serialNumber` iteratively based
// on the total count of 'E' characters in the `serialNumber`:
if count%2 == 0 {
for i := 0; i < ?; i++ {
md5Hash := ?
md5Hash.?

serialNumber = fmt.Sprintf("%x", md5Hash.Sum(nil)) // DO NOT delete!
}
}
return serialNumber
}

// DO NOT delete or modify the contents of the main() function!
func main() {
___

Create a free account to access the full topic