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"> <link href="/static/style.css" rel="stylesheet">
</head> </head>
<body> <body>
<form> <form method="post">
<label for="content"></label> <label for="content"></label>
<textarea id="content" name="content" placeholder="Your paste here"></textarea> <textarea id="content" name="content" placeholder="Your paste here"></textarea>
<nav> <nav>

View file

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