-
Notifications
You must be signed in to change notification settings - Fork 12
/
general.go
48 lines (37 loc) · 988 Bytes
/
general.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
package utils
import (
crand "crypto/rand"
"encoding/base64"
"math/rand"
"time"
"golang.org/x/crypto/curve25519"
)
func RandStringRunes(n int, letterRunes []rune) string {
if n <= 0 {
return ""
}
if len(letterRunes) == 0 {
letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_")
}
source := rand.NewSource(time.Now().UnixNano())
random := rand.New(source)
randomRunes := make([]rune, n)
for i := range randomRunes {
randomRunes[i] = letterRunes[random.Intn(len(letterRunes))]
}
return string(randomRunes)
}
func GenerateKey() (string, string, error) {
var priv, pub []byte
var err error
priv = make([]byte, curve25519.ScalarSize)
if _, err = crand.Read(priv); err != nil {
panic(err)
}
priv[0] &= 248
priv[31] &= 127 | 64
if pub, err = curve25519.X25519(priv, curve25519.Basepoint); err != nil {
panic(err)
}
return base64.StdEncoding.EncodeToString(priv[:]), base64.StdEncoding.EncodeToString(pub[:]), nil
}