github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/depends/util/password.go (about) 1 package util 2 3 import ( 4 "crypto/md5" 5 "encoding/hex" 6 "fmt" 7 "math/rand" 8 "time" 9 ) 10 11 func GenRandomPassword(size int, kind int) []byte { 12 ikind, kinds, result := kind, [][]int{{10, 48}, {26, 97}, {26, 65}}, make([]byte, size) 13 isAll := kind > 2 || kind < 0 14 rand.Seed(time.Now().UnixNano()) 15 for i := 0; i < size; i++ { 16 if isAll { 17 ikind = rand.Intn(3) 18 } 19 scope, base := kinds[ikind][0], kinds[ikind][1] 20 result[i] = uint8(base + rand.Intn(scope)) 21 } 22 return result 23 } 24 25 func HashOfAccountPassword(accountID string, password string) string { 26 return string(toMD5(toMD5([]byte(fmt.Sprintf("%s-%s", accountID, password))))) 27 } 28 29 func toMD5(src []byte) []byte { 30 m := md5.New() 31 _, _ = m.Write(src) 32 cipherStr := m.Sum(nil) 33 return []byte(hex.EncodeToString(cipherStr)) 34 } 35 36 func ExtractRawPasswordByAccountAndPassword(accountID, passwordMD5 string) (string, error) { 37 return "", nil // TODO 38 }