From 888fca0617aa9b99c8722b6476b3075225661202 Mon Sep 17 00:00:00 2001 From: Priyanshu Singh <123263608+dev-priyanshu15@users.noreply.github.com> Date: Sat, 12 Oct 2024 16:02:42 +0530 Subject: [PATCH] Create fletcher32.go add hacktoberfest-label please --- checksum/fletcher32.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 checksum/fletcher32.go diff --git a/checksum/fletcher32.go b/checksum/fletcher32.go new file mode 100644 index 000000000..86c880fe7 --- /dev/null +++ b/checksum/fletcher32.go @@ -0,0 +1,22 @@ +// Package checksum provides functions for calculating various checksum values. +package checksum + +import ( + "fmt" +) + +// Fletcher32 computes the Fletcher-32 checksum of the input data. +func Fletcher32(data []byte) uint32 { + var sum1, sum2 uint32 = 0xff, 0xff + for _, byte := range data { + sum1 = (sum1 + uint32(byte)) % 0xff + sum2 = (sum2 + sum1) % 0xff + } + return (sum2 << 8) | sum1 +} + +// Fletcher32Hex computes the Fletcher-32 checksum of a string and returns it as a hexadecimal string. +func Fletcher32Hex(input string) string { + fletcher := Fletcher32([]byte(input)) + return fmt.Sprintf("%08X", fletcher) +}