From 032a7e0c1188d3507b8d9a9571f2446a43cf775b Mon Sep 17 00:00:00 2001 From: msglm Date: Fri, 27 Oct 2023 07:08:25 -0500 Subject: apply https://github.com/42wim/matterbridge/pull/1864 --- vendor/maunium.net/go/mautrix/util/random.go | 65 ++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 vendor/maunium.net/go/mautrix/util/random.go (limited to 'vendor/maunium.net/go/mautrix/util/random.go') 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 +} -- cgit v1.2.3