Advertisement
Guest User

Checksum file

a guest
Oct 8th, 2015
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.54 KB | None | 0 0
  1. func ChecksumFile(fn string) string {
  2.     file, err := os.Open(fn)
  3.  
  4.     if err != nil {
  5.         panic(err.Error())
  6.     }
  7.  
  8.     defer file.Close()
  9.  
  10.     info, _ := file.Stat()
  11.  
  12.     filesize := info.Size()  
  13.  
  14.     blocks := uint64(math.Ceil(float64(filesize) / float64(filechunk)))
  15.  
  16.     hash := md5.New()
  17.  
  18.     for i := uint64(0); i < blocks; i++ {
  19.         blocksize := int(math.Min(filechunk, float64(filesize-int64(i*filechunk))))
  20.         buf := make([] byte, blocksize)
  21.  
  22.         file.Read(buf)
  23.         io.WriteString(hash, string(buf))
  24.     }
  25.  
  26.     return fmt.Sprintf("%x", hash.Sum(nil))
  27.  
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement