Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f6a471d

Browse files
committed
Merge pull request ericlagergren#108 from fangdingjun/checksum_share_code
complete the shaXXXsum commands
2 parents 3bc59da + ff20210 commit f6a471d

27 files changed

+1069
-200
lines changed

md5sum/calc_sum.go

Lines changed: 0 additions & 92 deletions
This file was deleted.

md5sum/checksum_common/calc_sum.go

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/*
2+
go checksum common
3+
4+
Copyright (c) 2014-2015 Dingjun Fang
5+
6+
This program is free software: you can redistribute it and/or modify
7+
it under the terms of the GNU General Public License version 3 as
8+
published by the Free Software Foundation.
9+
10+
This program is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
GNU General Public License for more details.
14+
15+
You should have received a copy of the GNU General Public License
16+
along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
19+
package checksum_common
20+
21+
import (
22+
"crypto/md5"
23+
"crypto/sha1"
24+
"crypto/sha256"
25+
"crypto/sha512"
26+
"fmt"
27+
//flag "github.com/ogier/pflag"
28+
"hash"
29+
"io"
30+
"os"
31+
"path/filepath"
32+
)
33+
34+
/*
35+
read from os.File and return the whole file's checksum
36+
*/
37+
func calc_checksum(fp io.Reader, t string) string {
38+
var m hash.Hash
39+
switch t {
40+
case "md5":
41+
m = md5.New()
42+
case "sha1":
43+
m = sha1.New()
44+
case "sha512":
45+
m = sha512.New()
46+
case "sha256":
47+
m = sha256.New()
48+
case "sha224":
49+
m = sha256.New224()
50+
case "sha384":
51+
m = sha512.New384()
52+
default:
53+
output_e("unknown type: %s\n", t)
54+
return ""
55+
}
56+
57+
/* issue:
58+
if fp is os.Stdin, there is no way to trigger EOF
59+
*/
60+
_, err := io.Copy(m, fp)
61+
62+
if err != nil {
63+
output_e("%ssum: %s\n", t, err.Error())
64+
return ""
65+
}
66+
67+
return fmt.Sprintf("%x", m.Sum(nil))
68+
}
69+
70+
/*
71+
generate the checksum for all of files from cmdline
72+
*/
73+
func gen_checksum(files []string, t string) bool {
74+
75+
has_error := false
76+
77+
for i := 0; i < len(files); i++ {
78+
fn := files[i]
79+
80+
/* stdin */
81+
if fn == "-" {
82+
sum := calc_checksum(os.Stdin, t)
83+
if sum != "" {
84+
fmt.Fprintf(os.Stdout, "%s *%s\n", sum, fn)
85+
} else {
86+
has_error = true
87+
}
88+
continue
89+
}
90+
91+
/* file */
92+
93+
/* extends file lists when filename contains '*' */
94+
filenames, _ := filepath.Glob(fn)
95+
if filenames == nil {
96+
filenames = append(filenames, fn)
97+
}
98+
99+
for _, f := range filenames {
100+
file, err := os.Open(f)
101+
if err != nil {
102+
has_error = true
103+
fmt.Fprintf(os.Stderr, "%ssum: %s\n", t, err.Error())
104+
continue
105+
}
106+
sum := calc_checksum(file, t)
107+
file.Close()
108+
if sum != "" {
109+
fmt.Fprintf(os.Stdout, "%s *%s\n", sum, f)
110+
} else {
111+
has_error = true
112+
}
113+
}
114+
}
115+
116+
return !has_error
117+
}
118+
119+
/*
120+
generate the checksum for given file list.
121+
122+
files: the file name lists to generate checksum
123+
124+
t: the type of checksum, md5 or sha1...
125+
126+
return false if there are some errors.
127+
128+
return true if there is no error.
129+
*/
130+
func GenerateChecksum(files []string, t string) bool {
131+
return gen_checksum(files, t)
132+
}

md5sum/check_sum.go renamed to md5sum/checksum_common/check_sum.go

Lines changed: 56 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
go md5sum
2+
go checksum common
33
44
Copyright (c) 2014-2015 Dingjun Fang
55
@@ -16,39 +16,45 @@
1616
along with this program. If not, see <http://www.gnu.org/licenses/>.
1717
*/
1818

19-
package main
19+
/*
20+
the core checksum implements of md5, sha1, sha224, sha256, sha384, sha512.
21+
*/
22+
package checksum_common
2023

2124
import (
2225
"bufio"
23-
flag "github.com/ogier/pflag"
26+
//flag "github.com/ogier/pflag"
2427
"io"
2528
"os"
29+
"path/filepath"
2630
"strings"
2731
)
2832

2933
/*
30-
check the md5sum for all of files from cmdline
34+
check the checksum for all of files
3135
*/
32-
func check_md5sum() bool {
33-
if len(flag.Args()) == 0 ||
34-
(len(flag.Args()) == 1 && flag.Args()[0] == "-") {
35-
36-
/* Known Issue:
37-
Ctrl+Z on cmd can not trigger io.EOF */
38-
return check_md5sum_f(os.Stdin)
39-
}
36+
func check_checksum(files []string, t string) bool {
4037

4138
has_err := false
4239

43-
for i := 0; i < len(flag.Args()); i++ {
44-
//output_e("use file: %s\n", flag.Args()[i])
45-
file, err := os.Open(flag.Args()[i])
40+
for i := 0; i < len(files); i++ {
41+
42+
/* stdin */
43+
if files[i] == "-" {
44+
if b := check_checksum_f(os.Stdin, t); !b {
45+
has_err = true
46+
}
47+
continue
48+
}
49+
50+
/* file */
51+
file, err := os.Open(files[i])
4652
if err != nil {
47-
output_e("md5sum: %s\n", err.Error())
53+
output_e("%ssum: %s\n", t, err.Error())
4854
has_err = true
4955
continue
5056
}
51-
if b := check_md5sum_f(file); !b {
57+
if b := check_checksum_f(file, t); !b {
5258
has_err = true
5359
}
5460
file.Close()
@@ -58,9 +64,9 @@ func check_md5sum() bool {
5864
}
5965

6066
/*
61-
process single md5 list file
67+
process single checksum list file
6268
*/
63-
func check_md5sum_f(fp io.Reader) bool {
69+
func check_checksum_f(fp io.Reader, t string) bool {
6470
has_err := false
6571
reader := bufio.NewReader(fp)
6672

@@ -82,7 +88,7 @@ func check_md5sum_f(fp io.Reader) bool {
8288
if err != nil {
8389
if err != io.EOF {
8490
has_err = true
85-
output_e("md5sum: %s\n", err.Error())
91+
output_e("%ssum: %s\n", t, err.Error())
8692
}
8793
break
8894
}
@@ -101,9 +107,9 @@ func check_md5sum_f(fp io.Reader) bool {
101107
fields := strings.Fields(ll)
102108

103109
if len(fields) != 2 {
104-
if *show_warn {
105-
output_e("md5sum: line: %d: improperly formatted MD5 checksum line\n",
106-
line_num)
110+
if show_warn {
111+
output_e("%ssum: line: %d: improperly formatted %s checksum line\n",
112+
t, line_num, strings.ToUpper(t))
107113
}
108114
continue
109115
}
@@ -115,15 +121,17 @@ func check_md5sum_f(fp io.Reader) bool {
115121
fn = fn[1:]
116122
}
117123

124+
fn = filepath.Clean(fn)
125+
118126
file, err := os.Open(fn)
119127
if err != nil {
120-
output_e("md5sum: %s\n", err.Error())
128+
output_e("%ssum: %s\n", t, err.Error())
121129
has_err = true
122130
errored += 1
123131
continue
124132
}
125133

126-
sum1 := calc_md5sum(file)
134+
sum1 := calc_checksum(file, t)
127135
file.Close()
128136

129137
total += 1
@@ -142,15 +150,32 @@ func check_md5sum_f(fp io.Reader) bool {
142150
}
143151
}
144152

145-
if failed > 0 && *show_warn {
146-
output_e("md5sum: WARNING: %d of %d computed checksums did NOT match\n",
147-
failed, total)
153+
if failed > 0 && show_warn {
154+
output_e("%ssum: WARNING: %d of %d computed checksums did NOT match\n",
155+
t, failed, total)
148156
}
149157

150-
if errored > 0 && *show_warn {
151-
output_e("md5sum: WARNING: %d of %d listed files could not be read\n",
152-
errored, total)
158+
if errored > 0 && show_warn {
159+
output_e("%ssum: WARNING: %d of %d listed files could not be read\n",
160+
t, errored, total)
153161
}
154162

155163
return !has_err
156164
}
165+
166+
/*
167+
read the file contains the checksum and check it
168+
169+
files: file name lists which contains the checksums.
170+
171+
t: the type of checksum, md5 or sha1...
172+
173+
return true if everything is ok
174+
175+
return false if there are some errors.
176+
*/
177+
func CompareChecksum(files []string, t string, output_message, output_warn bool) bool {
178+
no_output = !output_message
179+
show_warn = output_warn
180+
return check_checksum(files, t)
181+
}

0 commit comments

Comments
 (0)