计算文件的哈希值

#MD5 
func Md5File(path string) (string, error) {
    file, err := os.Open(path)
    defer file.Close() 
    if err != nil {
        return "",err 
    }

    h := md5.New()
    _, err = io.Copy(h,file)
    if err != nil {
        return "",err 
    }

    return fmt.Sprintf("%x",h.Sum(nil)), nil 
}

#SHA1     
func SHA1File(path string) (string, error) {
    file, err := os.Open(path)
    defer file.Close() 
    if err != nil {
        return "",err 
    }

    h := sha1.New() 
    _, err = io.Copy(h,file)
    if err != nil {
        return "",err 
    }
    return fmt.Sprintf("%x",h.Sum(nil)), nil 
}

#SHA256
func SHA256File(path string) (string, error) {
    file, err := os.Open(path)
    defer file.Close() 
    if err != nil {
        return "",err 
    }

    h := sha256.New() 
    _, err = io.Copy(h,file)
    if err != nil {
        return "",err 
    }
    return fmt.Sprintf("%x",h.Sum(nil)), nil 
}
文档更新时间: 2019-06-14 10:30   作者:月影鹏鹏