summaryrefslogtreecommitdiffstats
path: root/vendor/maunium.net/go/mautrix/util/random.go
diff options
context:
space:
mode:
authormsglm <msglm@techchud.xyz>2023-10-27 07:08:25 -0500
committermsglm <msglm@techchud.xyz>2023-10-27 07:08:25 -0500
commit032a7e0c1188d3507b8d9a9571f2446a43cf775b (patch)
tree2bd38c01bc7761a6195e426082ce7191ebc765a1 /vendor/maunium.net/go/mautrix/util/random.go
parent56e7bd01ca09ad52b0c4f48f146a20a4f1b78696 (diff)
downloadmatterbridge-msglm-032a7e0c1188d3507b8d9a9571f2446a43cf775b.tar.gz
matterbridge-msglm-032a7e0c1188d3507b8d9a9571f2446a43cf775b.tar.bz2
matterbridge-msglm-032a7e0c1188d3507b8d9a9571f2446a43cf775b.zip
apply https://github.com/42wim/matterbridge/pull/1864v1.26.0+0.1.0
Diffstat (limited to 'vendor/maunium.net/go/mautrix/util/random.go')
-rw-r--r--vendor/maunium.net/go/mautrix/util/random.go65
1 files changed, 65 insertions, 0 deletions
diff --git a/vendor/maunium.net/go/mautrix/util/random.go b/vendor/maunium.net/go/mautrix/util/random.go
new file mode 100644
index 00000000..944dd034
--- /dev/null
+++ b/vendor/maunium.net/go/mautrix/util/random.go
@@ -0,0 +1,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
+}