summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorsiddharth <s@ricketyspace.net>2021-09-04 18:35:06 -0400
committersiddharth <s@ricketyspace.net>2021-09-04 18:35:06 -0400
commit80f9ee3bc916c63a9bd54e0b0eadf95a7d86836b (patch)
tree182ad7d0999b33f3e37ab85e20ff999e11291762 /lib
parent880658d348c45655170564aab6787bd3fa2dad2e (diff)
lib: MTPpasswordResetToken
Diffstat (limited to 'lib')
-rw-r--r--lib/rng.go22
1 files changed, 22 insertions, 0 deletions
diff --git a/lib/rng.go b/lib/rng.go
index c1038f4..c48f719 100644
--- a/lib/rng.go
+++ b/lib/rng.go
@@ -142,3 +142,25 @@ func MTXORStream(stream, seed []byte) []byte {
}
return s
}
+
+func MTPasswordResetToken(seed uint32, length int) string {
+ if length < 16 {
+ length = 16 // Default length.
+ }
+
+ // Init MT19937.
+ mtR := new(MTRand)
+ mtR.Seed(seed)
+
+ n := uint32(0)
+ t := make([]byte, 0) // Token in bytes.
+ for i := 0; i < length; i++ {
+ if n == uint32(0) {
+ n = mtR.Extract()
+ }
+ t = append(t, byte(n&0xFF)) // Extract last 8 bits.
+ n = n >> 8 // Get rid of the last 8 bits.
+ }
+
+ return BytesToHexStr(t) // Return token as a hex string.
+}