Skip to content

Commit 888fca0

Browse files
Create fletcher32.go
add hacktoberfest-label please
1 parent 495cff8 commit 888fca0

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

checksum/fletcher32.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Package checksum provides functions for calculating various checksum values.
2+
package checksum
3+
4+
import (
5+
"fmt"
6+
)
7+
8+
// Fletcher32 computes the Fletcher-32 checksum of the input data.
9+
func Fletcher32(data []byte) uint32 {
10+
var sum1, sum2 uint32 = 0xff, 0xff
11+
for _, byte := range data {
12+
sum1 = (sum1 + uint32(byte)) % 0xff
13+
sum2 = (sum2 + sum1) % 0xff
14+
}
15+
return (sum2 << 8) | sum1
16+
}
17+
18+
// Fletcher32Hex computes the Fletcher-32 checksum of a string and returns it as a hexadecimal string.
19+
func Fletcher32Hex(input string) string {
20+
fletcher := Fletcher32([]byte(input))
21+
return fmt.Sprintf("%08X", fletcher)
22+
}

0 commit comments

Comments
 (0)