-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathGenerateSha256Base64Hex.go
70 lines (57 loc) · 2.02 KB
/
GenerateSha256Base64Hex.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
// https://replit.com/@javacrypto/CpcGoGenerateSha256Base64Hex#main.go
package main
import (
"crypto/sha256"
"encoding/hex"
"encoding/base64"
"fmt"
"strconv"
)
func main() {
fmt.Println("Generate a SHA-256 hash, Base64 en- and decoding and hex conversions")
plaintext := string("The quick brown fox jumps over the lazy dog")
fmt.Println("plaintext: " + plaintext)
sha256Value := CalculateSha256(plaintext)
sha256Length := len(sha256Value)
fmt.Println("sha256Value (hex) length: " + strconv.Itoa(sha256Length) + " data: " + BytesToHex(sha256Value))
sha256Base64 := Base64Encoding(sha256Value)
fmt.Println("sha256Value (base64) " + sha256Base64)
sha256ValueDecoded := Base64Decoding(sha256Base64)
sha256ValueDecodedLength := len(sha256ValueDecoded)
fmt.Println("sha256Base64 decoded to a byte array:")
fmt.Println("sha256Value (hex) length: " + strconv.Itoa(sha256ValueDecodedLength) + " data: " + BytesToHex(sha256ValueDecoded))
sha256HexString := "d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592"
sha256Hex := HexStringToByteArray(sha256HexString)
sha256HexLength := len(sha256Hex)
fmt.Println("sha256HexString converted to a byte array:")
fmt.Println("sha256Value (hex) length: " + strconv.Itoa(sha256HexLength) + " data: " + BytesToHex(sha256Hex))
}
func CalculateSha256(dataToHash string)([]byte){
msgHash := sha256.New()
_, err := msgHash.Write([]byte(dataToHash))
if err != nil {
panic(err)
}
msgHashSum := msgHash.Sum(nil)
return msgHashSum
}
func BytesToHex(input []byte)(string){
return hex.EncodeToString(input)
}
func HexStringToByteArray(input string)([]byte){
data, err := hex.DecodeString(input)
if err != nil {
panic(err)
}
return data;
}
func Base64Encoding(input []byte)(string) {
return base64.StdEncoding.EncodeToString(input)
}
func Base64Decoding(input string)([]byte) {
data, err := base64.StdEncoding.DecodeString(input)
if err != nil {
return data
}
return data
}