Created
August 6, 2018 15:43
-
-
Save evanj/6f4e12a1eb2f96ed9460c52ff2103242 to your computer and use it in GitHub Desktop.
Prints crc32c hashes in the same format as Google Cloud storage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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)) | |
} | |
} |
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
Thanks!