Golang--计算文件的MD5值
参考:
http://blog.csdn.net/u014029783/article/details/53762363
用法:
$ go run 01.go -f 1.txt
b9d228f114d3f42e82c6a0315dd21a3a 1.txt
$ go run 01.go -d tmp
503ff3936aeaf06adffe610788c7c091 tmp\wswFileServer5\files\01.md
3cda02c0f373006cebb29438bf0b01c6 tmp\wswFileServer5\main.go
86d5206af37b6bcea4d24b54336eee6b tmp\wswFileServer5\static\js\jquery-2.1.3.min.js
1ae0e64754a542cbea996dec63c326fd tmp\wswFileServer5\static\js\bootstrap.min.js
13986017db1f0f1010ed8ff4c81a0c3a tmp\wswFileServer5\README.md
c93ebb8bb2ae3bc85ac9de96ef6bb827 tmp\wswFileServer5\files\01.go
b9d228f114d3f42e82c6a0315dd21a3a tmp\wswFileServer5\files\1.txt
2b04df3ecc1d94afddff082d139c6f15 tmp\wswFileServer5\files\Koala.jpg
c43eeae863aad788074967b8cabd2dd7 tmp\wswFileServer5\index.html
bb884d3b6b6b09481c5dc25fb4fac7e5 tmp\wswFileServer5\static\css\bootstrap.min.css
$ go run 01.go -d tmp -merge
50f235f6435a3cc6700ca68844ced4c2 tmp
代码:
package main
import (
"crypto/md5"
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
)
//初始化变量
var directory, file *string
var merge *bool
//定义init函数,主要是flag的3个参数
func init() {
directory = flag.String("d", "", "The directory contains all the files that need to calculate the md5 value")
file = flag.String("f", "", "The file that need to caclulate the md5 value")
merge = flag.Bool("merge", false, "Merging all md5 values to one (Folder type only)")
}
func main() {
flag.Parse()
//返回Usage
if *directory == "" && *file == "" {
flag.Usage()
return
}
if *file != "" {
result, err := Md5SumFile(*file)
if err != nil {
panic(err)
}
fmt.Printf("%x %s\n", result, *file) //这里是*file
}
if *directory != "" {
result, err := Md5SumFolder(*directory)
if err != nil {
panic(err)
}
// 开启merge,则只计算总的MD5
if *merge == true {
var s string
for _, v := range result {
s += fmt.Sprintf("%x", v)
}
fmt.Printf("%x %s\n", md5.Sum([]byte(s)), *directory)
} else {
for k, v := range result {
fmt.Printf("%x %s\n", v, k)
}
}
}
}
func Md5SumFile(file string) (value [md5.Size]byte, err error) {
data, err := ioutil.ReadFile(file)
if err != nil {
return
}
value = md5.Sum(data)
return value, nil
}
func Md5SumFolder(folder string) (map[string][md5.Size]byte, error) {
results := make(map[string][md5.Size]byte)
filepath.Walk(folder, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
//判断文件属性
if info.Mode().IsRegular() {
result, err := Md5SumFile(path)
if err != nil {
return err
}
results[path] = result
}
return nil
})
return results, nil
}
Golang--计算文件的MD5值的更多相关文章
- C#计算文件的MD5值实例
C#计算文件的MD5值实例 MD5 是 Message Digest Algorithm 5(信息摘要算法)的缩写,MD5 一种散列(Hash)技术,广泛用于加密.解密.数据签名和数据完整性校验等方面 ...
- c#计算文件的MD5值
代码: /// <summary> /// 计算文件的 MD5 值 /// </summary> /// <param name="fileName" ...
- 计算文件的MD5值和sha256值
1.计算文件的MD5值. 1)linux系统计算 MD5值:md5sum+文件名 sha256值:sha256su+文件名 2)windows系统计算 MD5值:利用Notepad++工具计算 sha ...
- 在.NET中计算文件的MD5值
更新记录 本文迁移自Panda666原博客,原发布时间:2021年7月2日. 直接上代码吧: using System; using System.IO; using System.Security. ...
- python计算文件的md5值
前言 最近要开发一个基于python的合并文件夹/目录的程序,本来的想法是基于修改时间的比较,即判断文件有没有改变,比较两个文件的修改时间即可.这个想法在windows的pc端下测试没有问题. 但是当 ...
- 计算文件的MD5值(Java & Rust)
Java public class TestFileMD5 { public final static String[] hexDigits = { "0", "1&qu ...
- 计算字符串和文件的MD5值
//计算字符串的MD5值 public string GetMD5(string sDataIn) { MD5CryptoServiceProvider md5 = new MD5CryptoServ ...
- 计算指定文件的MD5值
/// <summary> /// 计算指定文件的MD5值 /// </summary> /// <param name="fileName"> ...
- c# 计算字符串和文件的MD5值的方法
快速使用Romanysoft LAB的技术实现 HTML 开发Mac OS App,并销售到苹果应用商店中. <HTML开发Mac OS App 视频教程> 土豆网同步更新:http: ...
- C# 计算文件的 Hash 值
/// <summary> /// 提供用于计算指定文件哈希值的方法 /// <example>例如计算文件的MD5值: /// <code> /// String ...
随机推荐
- js中常用的Math方法总结
1.min()和max()方法 Math.min()用于确定一组数值中的最小值.Math.max()用于确定一组数值中的最大值. alert(Math.min(2,4,3,6,3,8,0,1,3)); ...
- RFC Transactional RFC (tRFC) queue RFC(qRFC) 概念
Transactional RFC When using transactional RFC (tRFC), the called function module is executed exactl ...
- 用js使得输入框input只能输入数字
JS判断只能是数字和小数点1.文本框只能输入数字代码(小数点也不能输入)<input onkeyup="this.value=this.value.replace(/\D/g, ...
- Python 3
#对于任意可迭代对象可使用序列拆分操作符*进行拆分 #可用iterable对分片进行赋值等操作(相当于del分片并把iterable插入其中,所以二者长度可以不同) #del取消对象引用与数据项之间的 ...
- (89C51)定时器计时1s
unsigned ; void initT1() { EA=; TH1=0xDC; TL1=0X00; TMOD=0x10; TR1=; ET1=; } { TH1=0xDC; TL1=0X00; c ...
- angularjs的directive详解
Directive(指令)笔者认为是AngularJ非常强大而有有用的功能之一.它就相当于为我们写了公共的自定义DOM元素或CLASS属性或ATTR属性,并且它不只是单单如此,你还可以在它的基础上来操 ...
- svn问题(队列)
svn执行clean up命令时报错"Previous operation has not finished; run 'cleanup' if it was interrupted&quo ...
- maven之(六)setting.xml的配置文件详解
setting.xml配置文件 maven的配置文件settings.xml存在于两个地方: 1.安装的地方:${M2_HOME}/conf/settings.xml 2.用户的目录:${user.h ...
- webpack学习笔记—webpack安装、基本配置
文章结构: 什么是webpack? 安装webpack 'webpack基本配置 一.什么是webpack? 在学习react时发现大部分文章都是react和webpack结合使用的,所以在学reac ...
- jQuery datepicker和jQuery validator 共用时bug
当我们给一个元素绑定一个datepick后又要对它用validator进行验证时会发现验证并没有成功 因为当点击该元素时候input弹出datepick的UI就已经失去了焦点它验证的仍然是前一个值, ...