Skip to content

Instantly share code, notes, and snippets.

@evanj
Created August 6, 2018 15:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evanj/6f4e12a1eb2f96ed9460c52ff2103242 to your computer and use it in GitHub Desktop.
Save evanj/6f4e12a1eb2f96ed9460c52ff2103242 to your computer and use it in GitHub Desktop.
Prints crc32c hashes in the same format as Google Cloud storage
// Prints CRC32C hashes in the same format as Google Cloud Storage
//
// Usage: crc32c (paths to hash)
// See also: gsutil hash
package main
import (
"encoding/base64"
"encoding/hex"
"fmt"
"hash/crc32"
"io"
"os"
)
var crc32cTable = crc32.MakeTable(crc32.Castagnoli)
func crc32c(r io.Reader) ([]byte, error) {
hash := crc32.New(crc32cTable)
_, err := io.Copy(hash, r)
if err != nil {
return nil, err
}
return hash.Sum(nil), nil
}
func main() {
if len(os.Args) <= 1 {
os.Args = append(os.Args, "-")
}
for _, path := range os.Args[1:] {
var input *os.File
var err error
if path == "-" {
input = os.Stdin
} else {
input, err = os.Open(path)
if err != nil {
panic(err)
}
}
value, err := crc32c(input)
err2 := input.Close()
if err != nil {
panic(err)
}
if err2 != nil {
panic(err)
}
fmt.Printf("%s: base64:%s hex:%s\n", path,
base64.StdEncoding.EncodeToString(value), hex.EncodeToString(value))
}
}
@saoron
Copy link

saoron commented Jan 18, 2023

Thanks!

@evanj
Copy link
Author

evanj commented Jan 19, 2023

Ha you are welcome, although I admit I no longer remember why this was useful to me at the time :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment