Checksum Verifier — Go source
Drag and drop a file to compute its MD5, SHA-1, SHA-256, and SHA-512 checksums. Paste an expected hash to verify integrity - detect tampered or corrupted downloads instantly. Runs entirely in your browser.
This is the Go implementation — the same logic the interactive tool runs, in a shareable, citable form.
// Package checksumverifier is the Go twin of CosmoDev's
// src/lib/checksum-verifier.ts (dual source: the web lib is TypeScript, the
// CLI lib is Go — kept in lock-step). Pure + deterministic, never panics.
// The table-driven tests in checksum_verifier_test.go share vectors with
// src/lib/checksum-verifier.test.ts so the two implementations are held to
// the same contract.
//
// The TS lib implements MD5 from scratch (RFC 1321) because the Web Crypto
// API does not expose MD5; Go's stdlib crypto/md5 plays that role here, and
// the SHA family maps onto crypto/sha1, crypto/sha256, crypto/sha512 — so
// this port wraps the stdlib digests and adds the same verification helpers.
package checksumverifier
import (
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"encoding/hex"
"fmt"
"hash"
"strings"
"unicode"
)
// ChecksumResult carries all four digests of one input, as lowercase hex.
// It mirrors the ChecksumResult interface in the TS lib.
type ChecksumResult struct {
MD5 string
SHA1 string
SHA256 string
SHA512 string
}
// HashAlgorithm names a supported checksum algorithm.
// It mirrors the HashAlgorithm union in the TS lib.
type HashAlgorithm string
const (
AlgoMD5 HashAlgorithm = "md5"
AlgoSHA1 HashAlgorithm = "sha1"
AlgoSHA256 HashAlgorithm = "sha256"
AlgoSHA512 HashAlgorithm = "sha512"
)
// MD5Hasher is an incremental MD5 hasher. It mirrors the MD5Hasher interface
// returned by createMD5() in the TS lib: absorb bytes in chunks of any size
// (no length limit), then read the lowercase hex digest. Unlike the TS
// original, DigestHex does not consume the hasher — the digest reflects
// everything absorbed so far. MD5 is only for integrity checks, not security.
type MD5Hasher struct {
h hash.Hash
}
// CreateMD5 creates an incremental MD5 hasher. It is the twin of createMD5().
func CreateMD5() *MD5Hasher {
return &MD5Hasher{h: md5.New()}
}
// Update absorbs bytes. Call repeatedly for chunked input (no length limit).
// hash.Hash's Write never returns an error for digest implementations.
func (m *MD5Hasher) Update(data []byte) {
_, _ = m.h.Write(data)
}
// DigestHex pads, appends the 64-bit length, and returns the lowercase hex
// digest — the twin of digestHex() in the TS lib.
func (m *MD5Hasher) DigestHex() string {
return hex.EncodeToString(m.h.Sum(nil))
}
// MD5 is a synchronous one-shot MD5 of data, as lowercase hex. It is the twin
// of md5() in the TS lib.
func MD5(data []byte) string {
hasher := CreateMD5()
hasher.Update(data)
return hasher.DigestHex()
}
// SHAHex digests data with the named algorithm and returns lowercase hex.
// It mirrors shaHex() in the TS lib: algorithm is the Web Crypto name
// ("SHA-1", "SHA-256", or "SHA-512"). Unlike the async TS original it cannot
// fail on a missing crypto provider, but it returns an error for an unknown
// algorithm name, where Web Crypto would reject.
func SHAHex(algorithm string, data []byte) (string, error) {
switch algorithm {
case "SHA-1":
sum := sha1.Sum(data)
return hex.EncodeToString(sum[:]), nil
case "SHA-256":
sum := sha256.Sum256(data)
return hex.EncodeToString(sum[:]), nil
case "SHA-512":
sum := sha512.Sum512(data)
return hex.EncodeToString(sum[:]), nil
default:
return "", fmt.Errorf("checksumverifier: unsupported algorithm %q (want \"SHA-1\", \"SHA-256\", or \"SHA-512\")", algorithm)
}
}
// ComputeChecksums computes all four checksums of data (MD5 + three SHA
// digests, all lowercase hex). It is the twin of computeChecksums() in the TS
// lib; the TS async/await shape collapses to a plain call in Go.
func ComputeChecksums(data []byte) ChecksumResult {
sha1Hex, _ := SHAHex("SHA-1", data) // fixed valid names — the
sha256Hex, _ := SHAHex("SHA-256", data) // error path is unreachable
sha512Hex, _ := SHAHex("SHA-512", data) // here, same as the TS lib
return ChecksumResult{
MD5: MD5(data),
SHA1: sha1Hex,
SHA256: sha256Hex,
SHA512: sha512Hex,
}
}
// lengthToAlgo maps hex-string length to algorithm. It mirrors
// LENGTH_TO_ALGO in the TS lib: 32=MD5, 40=SHA-1, 64=SHA-256, 128=SHA-512.
var lengthToAlgo = map[int]HashAlgorithm{
32: AlgoMD5,
40: AlgoSHA1,
64: AlgoSHA256,
128: AlgoSHA512,
}
// isHexLower reports whether s is a non-empty string of [0-9a-f] — the Go
// equivalent of the TS lib's /^[0-9a-f]+$/ test.
func isHexLower(s string) bool {
if s == "" {
return false
}
for _, r := range s {
isDigit := r >= '0' && r <= '9'
isLowerHex := r >= 'a' && r <= 'f'
if !isDigit && !isLowerHex {
return false
}
}
return true
}
// normalizeHash drops whitespace and ':' grouping, then lowercases. It is the
// twin of normalizeHash() in the TS lib (trim + /[\s:]/g removal + toLowerCase).
// JS \s additionally contains U+FEFF, which unicode.IsSpace does not, so that
// rune is removed explicitly to keep the character class identical.
func normalizeHash(hash string) string {
return strings.ToLower(strings.Map(func(r rune) rune {
if r == ':' || r == '\ufeff' || unicode.IsSpace(r) {
return -1
}
return r
}, hash))
}
// DetectHashAlgorithm detects the algorithm from the hex length:
// 32=MD5, 40=SHA-1, 64=SHA-256, 128=SHA-512. The returned bool is false when
// the input is not a recognizable hex hash of a supported length — the twin
// of the TS lib's null return.
func DetectHashAlgorithm(hash string) (HashAlgorithm, bool) {
h := normalizeHash(hash)
if !isHexLower(h) {
return "", false
}
algorithm, ok := lengthToAlgo[len(h)]
return algorithm, ok
}
// Verification is the outcome of a checksum comparison: which algorithm the
// expected hash was detected as, and whether it equals the computed digest.
// It mirrors the return shape of verifyChecksum() in the TS lib.
type Verification struct {
Algorithm HashAlgorithm
Match bool
}
// VerifyChecksum compares an expected hash against a computed result. The
// returned bool is false when the expected string is not a recognizable hex
// hash of a supported length (the TS lib returns null there). Matching is
// case-insensitive and ignores whitespace/':' grouping.
func VerifyChecksum(expected string, result ChecksumResult) (Verification, bool) {
algorithm, ok := DetectHashAlgorithm(expected)
if !ok {
return Verification{}, false
}
var computed string
switch algorithm {
case AlgoMD5:
computed = result.MD5
case AlgoSHA1:
computed = result.SHA1
case AlgoSHA256:
computed = result.SHA256
case AlgoSHA512:
computed = result.SHA512
}
return Verification{Algorithm: algorithm, Match: normalizeHash(expected) == computed}, true
}
Also available in 13 other languages
Every CosmoDev tool ships its pure logic in TypeScript (web) and Go (CLI), with authored implementations in a dozen-plus languages — the same contract, ported. Compare all languages side by side →