-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcryptoutils.go
110 lines (85 loc) · 3.41 KB
/
cryptoutils.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package main
import (
b64 "encoding/base64"
"errors"
"fmt"
"os"
"github.com/GoKillers/libsodium-go/cryptogenerichash"
"github.com/GoKillers/libsodium-go/cryptosign"
)
//SignSimple is a function that Signs a string based on Serect Key
func SignSimple(plainText string, secretKey string) (signString string, hashString string, errorValue error) {
sk64Decoded, _ := b64.StdEncoding.DecodeString(secretKey)
hashString, hashBytes := HashSimple(plainText)
signBytes, _ := cryptosign.CryptoSignDetached(hashBytes, sk64Decoded)
signString = b64.StdEncoding.EncodeToString([]byte(signBytes))
return signString, hashString, nil
}
//VerifySignSimple verifies signature, hash based on PublicKey
func VerifySignSimple(signText string, messageText string, hashInText string, publicKey string) (signVerified int, errorValue error) {
//Hash the message
sign64Decoded, _ := b64.StdEncoding.DecodeString(signText)
public64Decoded, _ := b64.StdEncoding.DecodeString(publicKey)
hashString, hashBytes := HashSimple(messageText)
if hashString != hashInText {
fmt.Println("Hash Check Failed")
return -1, errors.New("Hash Check Failed")
}
return cryptosign.CryptoSignVerifyDetached(sign64Decoded, hashBytes, public64Decoded), nil
}
//HashSimple produces a Hash of string
func HashSimple(message string) (hashString string, hashBytes []byte) {
//Pass Byte array to hash
m := make([]byte, len(message))
copy(m, message)
k := make([]byte, generichash.CryptoGenericHashKeyBytes())
hashBytes, _ = generichash.CryptoGenericHash(generichash.CryptoGenericHashBytes(), m, k)
hashString = b64.StdEncoding.EncodeToString(hashBytes)
return hashString, hashBytes
}
//HashFileSimple produces a Hash of a File
func HashFileSimple(inputFilePath string) (hashString string, hashBytes []byte, errorValue error) {
file, err := os.Open(inputFilePath)
if err != nil {
fmt.Println(err)
return "", nil, err
}
defer file.Close()
fileInfo, err := file.Stat()
if err != nil {
fmt.Println(err)
return "", nil, err
}
fileSize := fileInfo.Size()
buffer := make([]byte, fileSize)
bytesread, err := file.Read(buffer)
if err != nil {
fmt.Println(err)
return "", nil, err
}
fmt.Println("Bytes read: ", bytesread)
k := make([]byte, generichash.CryptoGenericHashKeyBytes())
hashBytes, _ = generichash.CryptoGenericHash(generichash.CryptoGenericHashBytes(), buffer, k)
hashString = b64.StdEncoding.EncodeToString(hashBytes)
return hashString, hashBytes, nil
}
//SignFileSimple signs a local file
func SignFileSimple(inputFilePath string, secretKey string) (signString string, hashString string, errorValue error) {
sk64Decoded, _ := b64.StdEncoding.DecodeString(secretKey)
hashString, hashBytes, _ := HashFileSimple(inputFilePath)
signBytes, _ := cryptosign.CryptoSignDetached(hashBytes, sk64Decoded)
signString = b64.StdEncoding.EncodeToString([]byte(signBytes))
return signString, hashString, nil
}
//VerifyFileSimple verifies a local file
func VerifyFileSimple(signText string, inputFilePath string, hashInText string, publicKey string) (signVerified int, errorValue error) {
//Hash the message
sign64Decoded, _ := b64.StdEncoding.DecodeString(signText)
public64Decoded, _ := b64.StdEncoding.DecodeString(publicKey)
hashString, hashBytes, _ := HashFileSimple(inputFilePath)
if hashString != hashInText {
fmt.Println("Hash Check Failed")
return -1, nil
}
return cryptosign.CryptoSignVerifyDetached(sign64Decoded, hashBytes, public64Decoded), nil
}