summaryrefslogtreecommitdiffstats
path: root/vendor/maunium.net/go/mautrix/util/random.go
blob: 944dd034a7cbb01b5ef4333671e94dceb62883d5 (plain) (blame)
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
// Copyright (c) 2022 Tulir Asokan
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

package util

import (
	"crypto/rand"
	"encoding/base64"
	"hash/crc32"
	"strings"
	"unsafe"
)

func RandomBytes(n int) []byte {
	data := make([]byte, n)
	_, err := rand.Read(data)
	if err != nil {
		panic(err)
	}
	return data
}

var letters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"

// RandomString generates a random string of the given length.
func RandomString(n int) string {
	if n <= 0 {
		return ""
	}
	base64Len := n
	if n%4 != 0 {
		base64Len += 4 - (n % 4)
	}
	decodedLength := base64.RawStdEncoding.DecodedLen(base64Len)
	output := make([]byte, base64Len)
	base64.RawStdEncoding.Encode(output, RandomBytes(decodedLength))
	for i, char := range output {
		if char == '+' || char == '/' {
			_, err := rand.Read(output[i : i+1])
			if err != nil {
				panic(err)
			}
			output[i] = letters[int(output[i])%len(letters)]
		}
	}
	return (*(*string)(unsafe.Pointer(&output)))[:n]
}

func base62Encode(val uint32, minWidth int) string {
	var buf strings.Builder
	for val > 0 {
		buf.WriteByte(letters[val%62])
		val /= 62
	}
	return strings.Repeat("0", minWidth-buf.Len()) + buf.String()
}

func RandomToken(namespace string, randomLength int) string {
	token := namespace + "_" + RandomString(randomLength)
	checksum := base62Encode(crc32.ChecksumIEEE([]byte(token)), 6)
	return token + "_" + checksum
}