mirror of
https://github.com/rwinkhart/libmutton.git
synced 2026-09-01 22:57:25 -04:00
Allow more control over string generation to improve generated password compatibility
This commit is contained in:
+15
-10
@@ -130,22 +130,27 @@ func EntryAddPrecheck(targetLocation string) uint8 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// StringGen generates a random string of a specified length and complexity.
|
// StringGen generates a random string of a specified length and complexity.
|
||||||
// Requires: complexity (minimum percentage of special characters to be returned in the generated string; only impacts complex strings),
|
// Requires: complexity (minimum percentage of special characters to be returned in the generated string; set to 0 to generate a simple string),
|
||||||
// safeForFileName: (if true, the generated string will only contain special characters that are safe for file names; only impacts complex strings).
|
// complexCharsetLevel (0 = safe for filenames, 1 = safe for most password entries, 2 = safe only for well-made password entries)
|
||||||
func StringGen(length int, complex bool, complexity float64, safeForFileName bool) string {
|
func StringGen(length int, complexity float64, complexCharsetLevel uint8) string {
|
||||||
var actualSpecialChars int // track the number of special characters in the generated string
|
var actualSpecialChars int // track the number of special characters in the generated string
|
||||||
var minSpecialChars int // track the minimum number of special characters to accept
|
var minSpecialChars int // track the minimum number of special characters to accept
|
||||||
var extendedCharset string // additions to character set used for complex strings
|
var extendedCharset string // additions to character set used for complex strings
|
||||||
|
|
||||||
charset := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" // default character set used for all strings
|
charset := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" // default character set used for all strings
|
||||||
const extendedCharsetFiles = "!#$%&'()+,-.;=@[]^_`{}~" // additional special characters for complex strings (safe in file names)
|
const extendedCharsetFiles = "!#$%&+,-.;=@_~^()[]{}`'" // additional special characters for complex strings (safe in file names)
|
||||||
const extendedCharsetPassword = "\"*:><?/\\|" // additional special characters for complex strings (NOT safe in file names)
|
const extendedCharsetMostPassword = "*:><?|" // additional special characters for complex strings (NOT safe in file names)
|
||||||
if complex {
|
const extendedCharsetSpecialPassword = "\"/\\" // additional special characters for complex strings (NOT safe in file names)
|
||||||
|
|
||||||
|
if complexity > 0 {
|
||||||
minSpecialChars = int(math.Round(float64(length) * complexity)) // determine minimum number of special characters to accept
|
minSpecialChars = int(math.Round(float64(length) * complexity)) // determine minimum number of special characters to accept
|
||||||
if !safeForFileName {
|
switch complexCharsetLevel {
|
||||||
extendedCharset = extendedCharsetFiles + extendedCharsetPassword
|
case 0:
|
||||||
} else {
|
|
||||||
extendedCharset = extendedCharsetFiles
|
extendedCharset = extendedCharsetFiles
|
||||||
|
case 1:
|
||||||
|
extendedCharset = extendedCharsetMostPassword + extendedCharsetFiles[:len(extendedCharsetFiles)-9]
|
||||||
|
case 2:
|
||||||
|
extendedCharset = extendedCharsetFiles + extendedCharsetMostPassword + extendedCharsetSpecialPassword
|
||||||
}
|
}
|
||||||
charset += extendedCharset
|
charset += extendedCharset
|
||||||
}
|
}
|
||||||
@@ -160,7 +165,7 @@ func StringGen(length int, complex bool, complexity float64, safeForFileName boo
|
|||||||
}
|
}
|
||||||
|
|
||||||
// return early if the string is not complex
|
// return early if the string is not complex
|
||||||
if !complex {
|
if complexity == 0 {
|
||||||
return string(result)
|
return string(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -17,7 +17,7 @@ import (
|
|||||||
func DeviceIDGen(oldDeviceID string) (string, string) {
|
func DeviceIDGen(oldDeviceID string) (string, string) {
|
||||||
// generate new device ID
|
// generate new device ID
|
||||||
deviceIDPrefix, _ := os.Hostname()
|
deviceIDPrefix, _ := os.Hostname()
|
||||||
deviceIDSuffix := core.StringGen(rand.Intn(32)+48, true, 0.2, true) + "-" + strconv.FormatInt(time.Now().Unix(), 10)
|
deviceIDSuffix := core.StringGen(rand.Intn(32)+48, 0.2, 0) + "-" + strconv.FormatInt(time.Now().Unix(), 10)
|
||||||
newDeviceID := deviceIDPrefix + "-" + deviceIDSuffix
|
newDeviceID := deviceIDPrefix + "-" + deviceIDSuffix
|
||||||
|
|
||||||
// create new device ID file (locally)
|
// create new device ID file (locally)
|
||||||
|
|||||||
Reference in New Issue
Block a user