change(backend): use crypto/rand for secret

This commit is contained in:
Ada 2023-10-03 23:20:31 +02:00
parent fe202b8e06
commit 7d45a1792b
2 changed files with 11 additions and 9 deletions

View file

@ -9,7 +9,7 @@
<link href="/static/style.css" rel="stylesheet">
</head>
<body>
<form>
<form method="post">
<label for="content"></label>
<textarea id="content" name="content" placeholder="Your paste here"></textarea>
<nav>

View file

@ -1,7 +1,9 @@
package main
import (
"math/rand"
"crypto/rand"
"encoding/hex"
mathrand "math/rand"
)
func generateUrl() string {
@ -9,21 +11,21 @@ func generateUrl() string {
listChars := []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
b := make([]rune, length)
for i := range b {
b[i] = listChars[rand.Intn(len(listChars))]
b[i] = listChars[mathrand.Intn(len(listChars))]
}
return string(b)
}
func generateSecret() string {
length := 32
listChars := []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
b := make([]rune, length)
for i := range b {
b[i] = listChars[rand.Intn(len(listChars))]
key := make([]byte, 32)
_, err := rand.Read(key)
if err != nil {
// handle error here
}
return string(b)
secret := hex.EncodeToString(key)
return secret
}
func urlExist(url string) bool {