main.go 910 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package main
  2. import (
  3. "bufio"
  4. "crypto/md5"
  5. "crypto/sha1"
  6. "crypto/sha256"
  7. "crypto/sha512"
  8. "flag"
  9. "fmt"
  10. "hash"
  11. "io"
  12. "os"
  13. )
  14. var (
  15. inputFile = flag.String("f", "", "File for checksum calculating")
  16. algo = flag.String("algo", "md5", "Algorithm checksum calculation. Allowed values: md5, sha1, sha256, sha512.")
  17. )
  18. func main() {
  19. flag.Parse()
  20. if *inputFile == "" {
  21. flag.PrintDefaults()
  22. os.Exit(0)
  23. }
  24. f, err := os.Open(*inputFile)
  25. if err != nil {
  26. fmt.Printf("Error open file: %v\n", err)
  27. os.Exit(1)
  28. }
  29. defer f.Close()
  30. reader := bufio.NewReader(f)
  31. var hash hash.Hash
  32. switch *algo {
  33. case "md5":
  34. hash = md5.New()
  35. case "sha256":
  36. hash = sha256.New()
  37. case "sha1":
  38. hash = sha1.New()
  39. case "sha512":
  40. hash = sha512.New()
  41. }
  42. _, err = io.Copy(hash, reader)
  43. if err != nil {
  44. fmt.Printf("Error reading file: %v\n", err)
  45. os.Exit(1)
  46. }
  47. fmt.Printf("%s: %x", *algo, hash.Sum(nil))
  48. }