-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
84 lines (69 loc) · 1.84 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package main
import (
"errors"
"flag"
"fmt"
"log"
"os"
"github.com/bzhn/buid/random"
file "github.com/bzhn/buid/file"
tm "github.com/bzhn/buid/time"
)
const (
variationF = "f" // stands for files
// hash to use
sha256 = 1
sha512 = 2
xxh64 = 3
SHA256HASHnum = 0 // 00xx
SHA512HASHnum = 4 // 01xx
XXH64HASHnum = 8 // 10xx
)
var hashnum uint
var pathtofile string
func init() {
flag.UintVar(&hashnum, "hash", 1, "Hash to use\n0 for SHA256\n1 for SHA512\n2 for XXH64")
flag.StringVar(&pathtofile, "path", `C:\Program Files\7-Zip\7z.exe`, "Path to file")
flag.Parse()
}
func main() {
uuid, err := UUID(uint8(hashnum), pathtofile)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s\n", uuid)
}
func UUID(hashToUse uint8, pathToFile string) (string, error) {
var variation, randomhex, hashPart string
variation = variationF
// Check that hashToUse is between 1 and 3
if hashToUse < 1 || hashToUse > 3 {
return "", errors.New("Number of the hash to use is not in the range between 1 and 3")
}
// TODO: Check that file exists and accessible
f, err := os.Open(pathToFile)
defer f.Close()
fstat, err := f.Stat()
if err != nil {
return "", err
}
fileSize := fstat.Size()
fileContent, err := os.ReadFile(pathToFile)
if err != nil {
return "", err
}
switch hashToUse {
case sha256:
randomhex = fmt.Sprintf("%x", SHA256HASHnum+random.HexChar())
hashPart = file.Sha256part(fileContent)
case sha512:
randomhex = fmt.Sprintf("%x", SHA512HASHnum+random.HexChar())
hashPart = file.Sha512part(fileContent)
case xxh64:
randomhex = fmt.Sprintf("%x", XXH64HASHnum+random.HexChar())
hashPart = file.Xxhash64part(fileContent)
default:
return "", errors.New("wrong number of hash to use")
}
return fmt.Sprintf("%s-%s%s%s%s-%s", tm.Timestamp(), variation, randomhex, file.SizeApprox(fileSize), file.SizeMod(fileSize), hashPart), nil
}