2014-12-20 19:01:27 -05:00
|
|
|
package md5
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/md5"
|
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Sum(reader io.Reader) ([]byte, error) {
|
|
|
|
hash := md5.New()
|
|
|
|
var err error
|
2014-12-21 05:12:30 -05:00
|
|
|
var length int
|
2014-12-20 19:01:27 -05:00
|
|
|
for err == nil {
|
|
|
|
byteBuffer := make([]byte, 1024*1024)
|
|
|
|
length, err = reader.Read(byteBuffer)
|
2014-12-21 05:12:30 -05:00
|
|
|
// While hash.Write() wouldn't mind a Nil byteBuffer
|
|
|
|
// It is necessary for us to verify this and break
|
|
|
|
if length == 0 {
|
|
|
|
break
|
|
|
|
}
|
2014-12-20 19:01:27 -05:00
|
|
|
byteBuffer = byteBuffer[0:length]
|
|
|
|
hash.Write(byteBuffer)
|
|
|
|
}
|
|
|
|
if err != io.EOF {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return hash.Sum(nil), nil
|
|
|
|
}
|