Skip to content

Instantly share code, notes, and snippets.

@geo-stanciu
Last active September 13, 2018 08:34
Show Gist options
  • Select an option

  • Save geo-stanciu/bba11523e5489861b354fc84e05f2025 to your computer and use it in GitHub Desktop.

Select an option

Save geo-stanciu/bba11523e5489861b354fc84e05f2025 to your computer and use it in GitHub Desktop.
golang - sha256 files
/*
Copyright (c) 2018, Gheorghita Stanciu gheorghita(dot)stanciu(at)gmail(dot)com
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
The views and conclusions contained in the software and documentation are those
of the authors and should not be interpreted as representing official policies,
either expressed or implied, of the FreeBSD Project.
*/
package main
import (
"crypto/sha256"
"encoding/hex"
"fmt"
"io"
"log"
"os"
"path/filepath"
"sort"
"strings"
)
type fileSum struct {
filename string
sha256 string
}
func main() {
dir := "./"
if len(os.Args) >= 2 {
dir = os.Args[1]
}
dir = getAbsPath(dir)
if _, err := os.Stat(dir); os.IsNotExist(err) {
log.Fatalf("Directory \"%s\" does not exist", dir)
return
}
if fi, _ := os.Stat(dir); !fi.IsDir() {
log.Fatalf("\"%s\" if not a directory", dir)
return
}
docs, err := filepath.Glob(dir + "/" + "*.doc")
if err != nil {
log.Fatal(err)
return
}
docxs, err := filepath.Glob(dir + "/" + "*.docx")
if err != nil {
log.Fatal(err)
return
}
zips, err := filepath.Glob(dir + "/" + "*.zip")
if err != nil {
log.Fatal(err)
return
}
rars, err := filepath.Glob(dir + "/" + "*.rar")
if err != nil {
log.Fatal(err)
return
}
readmes, err := filepath.Glob(dir + "/" + "Readme*.txt")
if err != nil {
log.Fatal(err)
return
}
files := append(docs, docxs...)
files = append(files, zips...)
files = append(files, rars...)
files = append(files, readmes...)
sort.Slice(files, func(i, j int) bool {
a := files[i]
b := files[j]
return a < b
})
maxlen := 0
checksum := make([]fileSum, len(files))
for i, f := range files {
sum, err := sha256sum(f)
if err != nil {
log.Fatal(err)
return
}
checksum[i] = fileSum{filename: filepath.Base(f), sha256: sum}
l := len(checksum[i].filename)
if maxlen < l {
maxlen = l
}
}
for _, f := range checksum {
fmt.Printf("%s %s\r\n", rightPad(f.filename, " ", maxlen), f.sha256)
}
}
func leftPad(s string, padStr string, pLen int) string {
return strings.Repeat(padStr, pLen-len(s)) + s
}
func rightPad(s string, padStr string, pLen int) string {
return s + strings.Repeat(padStr, pLen-len(s))
}
func sha256sum(filePath string) (result string, err error) {
file, err := os.Open(filePath)
if err != nil {
return
}
defer file.Close()
hash := sha256.New()
_, err = io.Copy(hash, file)
if err != nil {
return
}
result = hex.EncodeToString(hash.Sum(nil))
return
}
func getAbsPath(dir string) string {
directory := dir
if len(directory) == 0 {
directory = "./"
}
directory, err := filepath.Abs(directory)
if err != nil {
log.Fatal(err)
return "./"
}
if directory[len(directory)-1:] == "/" || directory[len(directory)-1:] == "\\" {
directory = directory[0 : len(directory)-1]
}
return directory
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment