switch from math/rand to crypto/rand package

This commit is contained in:
itchyny 2023-04-09 12:25:22 +09:00
parent 49d37b4903
commit e81d936dd5
2 changed files with 7 additions and 9 deletions

View file

@ -4,12 +4,10 @@ import (
"errors"
"flag"
"fmt"
"math/rand"
"os"
"os/exec"
"runtime"
"strings"
"time"
_ "github.com/mattn/getwild"
"github.com/mattn/go-tty"
@ -23,10 +21,6 @@ const version = "0.1.4"
var revision = "HEAD"
func init() {
rand.Seed(time.Now().UnixNano())
}
func main() {
os.Exit(run(os.Args[1:]))
}

10
mmv.go
View file

@ -2,10 +2,10 @@
package mmv
import (
"math/rand"
"crypto/rand"
"encoding/base64"
"os"
"path/filepath"
"strconv"
"strings"
)
@ -251,8 +251,12 @@ func buildRenames(files map[string]string) ([]rename, error) {
// create a temporary path where there is no file currently
func temporaryPath(dir string) (string, error) {
bs := make([]byte, 16)
for i := 0; i < 256; i++ {
path := filepath.Join(dir, strconv.FormatUint(rand.Uint64()|1<<60, 16))
if _, err := rand.Read(bs); err != nil {
return "", err
}
path := filepath.Join(dir, base64.RawURLEncoding.EncodeToString(bs))
if _, err := os.Stat(path); err != nil && os.IsNotExist(err) {
return path, nil
}