You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
28 lines
512 B
28 lines
512 B
10 years ago
|
package md5
|
||
|
|
||
|
import (
|
||
|
"crypto/md5"
|
||
|
"io"
|
||
|
)
|
||
|
|
||
|
func Sum(reader io.Reader) ([]byte, error) {
|
||
|
hash := md5.New()
|
||
|
var err error
|
||
10 years ago
|
var length int
|
||
10 years ago
|
for err == nil {
|
||
|
byteBuffer := make([]byte, 1024*1024)
|
||
|
length, err = reader.Read(byteBuffer)
|
||
10 years ago
|
// While hash.Write() wouldn't mind a Nil byteBuffer
|
||
|
// It is necessary for us to verify this and break
|
||
|
if length == 0 {
|
||
|
break
|
||
|
}
|
||
10 years ago
|
byteBuffer = byteBuffer[0:length]
|
||
|
hash.Write(byteBuffer)
|
||
|
}
|
||
|
if err != io.EOF {
|
||
|
return nil, err
|
||
|
}
|
||
|
return hash.Sum(nil), nil
|
||
|
}
|