Removed deprecated ioutil and switched to more secure crypto/rand

This commit is contained in:
Tracker-Friendly 2024-04-27 10:35:16 +01:00
parent 5d3022f1cc
commit f8cb4c3f86
1 changed files with 10 additions and 5 deletions

15
main.go
View File

@ -1,13 +1,12 @@
package main package main
import ( import (
"crypto/rand"
"crypto/sha256" "crypto/sha256"
"database/sql" "database/sql"
"encoding/base64" "encoding/base64"
"encoding/hex" "encoding/hex"
"fmt" "fmt"
"io/ioutil"
"math/rand"
"os" "os"
"regexp" "regexp"
"strconv" "strconv"
@ -25,12 +24,18 @@ const SALT_CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567
func genSalt(length int) string { func genSalt(length int) string {
if length <= 0 { if length <= 0 {
panic("Salt length must be at least 1.") fmt.Println("[ERROR] Known in genSalt() at", strconv.FormatInt(time.Now().Unix(), 10)+":", "Salt length must be at least one.")
} }
salt := make([]byte, length) salt := make([]byte, length)
randomBytes := make([]byte, length)
_, err := rand.Read(randomBytes)
if err != nil {
fmt.Println("[ERROR] Unknown in genSalt() at", strconv.FormatInt(time.Now().Unix(), 10)+":", err)
}
for i := range salt { for i := range salt {
salt[i] = SALT_CHARS[rand.Intn(len(SALT_CHARS))] salt[i] = SALT_CHARS[int(randomBytes[i])%len(SALT_CHARS)]
} }
return string(salt) return string(salt)
} }
@ -150,7 +155,7 @@ func generateDB() error {
} }
defer db.Close() defer db.Close()
schemaBytes, err := ioutil.ReadFile("schema.sql") schemaBytes, err := os.ReadFile("schema.sql")
if err != nil { if err != nil {
return err return err
} }