This repository has been archived by the owner on Jan 20, 2023. It is now read-only.
/
pkg.go
82 lines (72 loc) · 1.75 KB
/
pkg.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
package crypt
import (
"bytes"
"crypto/md5"
"io"
"strings"
"github.com/gtank/cryptopasta"
"github.com/nikolay-turpitko/structor/funcs/use"
)
// Pkg contains custom functions defined by this package.
var Pkg = use.FuncMap{
// func rot13(s string) string
// Performs simple rot13 obfuscation.
"rot13": rot13,
// func md5(r io.Reader) ([]byte, error)
// Calculates md5 checksum of io.Reader's content and returns it as []byte.
"md5": md5Sum,
// func rndKey() []byte
// Returns random encryption key for "aes"/"unaes".
// See "github.com/gtank/cryptopasta".NewEncryptionKey().
"rndKey": rndKey,
// func aes(key, plain []byte) ([]byte, error)
// Encrypts data using 256-bit AES-GCM. See "github.com/gtank/cryptopasta".Encrypt().
"aes": aes,
// func unaes(key, cipher []byte) ([]byte, error)
// Decrypts data using 256-bit AES-GCM. See "github.com/gtank/cryptopasta".Decrypt().
"unaes": unaes,
}
func rot13(s string) string { return strings.Map(mapRot13, s) }
func mapRot13(r rune) rune {
if r >= 'a' && r <= 'z' {
if r >= 'm' {
return r - 13
}
return r + 13
} else if r >= 'A' && r <= 'Z' {
if r >= 'M' {
return r - 13
}
return r + 13
}
return r
}
func md5Sum(r io.Reader) ([]byte, error) {
h := md5.New()
var b bytes.Buffer
_, err := io.Copy(&b, r)
if err != nil {
return nil, err
}
_, err = h.Write(b.Bytes())
if err != nil {
return nil, err
}
return h.Sum(nil), nil
}
func rndKey() []byte {
k := cryptopasta.NewEncryptionKey()
key := make([]byte, 32)
copy(key, k[:])
return key
}
func aes(key, plain []byte) ([]byte, error) {
var k [32]byte
copy(k[:], key)
return cryptopasta.Encrypt(plain, &k)
}
func unaes(key, cipher []byte) ([]byte, error) {
var k [32]byte
copy(k[:], key)
return cryptopasta.Decrypt(cipher, &k)
}