Working on v2 algorithm

This commit is contained in:
Brian Buller 2024-06-20 12:04:42 -05:00
parent 6a28279c6d
commit 8a98b4940d

View File

@ -16,18 +16,20 @@ type Passage struct {
valids int valids int
length int length int
pin string pin string
requireEachCharSet bool
} }
func CreatePassage(pin string) *Passage { func CreatePassage(pin string) *Passage {
p := new(Passage) p := new(Passage)
p.length = 20 p.length = 20
p.valids = UseLower | UseUpper | UseNumber p.valids = UseLower | UseUpper | UseNumber | UseSymbol
p.pin = pin p.pin = pin
return p return p
} }
func (p *Passage) SetPin(pin string) { func (p *Passage) SetRequireEachCharSet(r bool) {
p.pin = pin p.requireEachCharSet = r
} }
func (p *Passage) EnableChars(which int) { func (p *Passage) EnableChars(which int) {
@ -42,35 +44,76 @@ func (p *Passage) SetLength(length int) {
p.length = length p.length = length
} }
func (p *Passage) GetLowers() string {
return "abcdefghijklmnopqrstuvwxyz"
}
func (p *Passage) GetUppers() string {
return "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
}
func (p *Passage) GetNumbers() string {
return "1234567890"
}
func (p *Passage) GetSymbols() string {
return "~!@#$%^&*()[]{}|;:,<.>/?"
}
func (p *Passage) GetValidChars() string { func (p *Passage) GetValidChars() string {
validChars := "" validChars := ""
if p.valids&UseLower == UseLower { if p.valids&UseLower == UseLower {
validChars += "abcdefghijklmnopqrstuvwxyz" validChars += p.GetLowers()
} }
if p.valids&UseUpper == UseUpper { if p.valids&UseUpper == UseUpper {
validChars += "ABCDEFGHIJKLMNOPQRSTUVWXYZ" validChars += p.GetUppers()
} }
if p.valids&UseNumber == UseNumber { if p.valids&UseNumber == UseNumber {
validChars += "1234567890" validChars += p.GetNumbers()
} }
if p.valids&UseSymbol == UseSymbol { if p.valids&UseSymbol == UseSymbol {
validChars += "~!@#$%^&*()[]{}|;:,<.>/?" validChars += p.GetSymbols()
}
return validChars
}
func (p *Passage) GetValidCharSets() []string {
var validChars []string
if p.valids&UseLower == UseLower {
validChars = append(validChars, p.GetLowers())
}
if p.valids&UseUpper == UseUpper {
validChars = append(validChars, p.GetUppers())
}
if p.valids&UseNumber == UseNumber {
validChars = append(validChars, p.GetNumbers())
}
if p.valids&UseSymbol == UseSymbol {
validChars = append(validChars, p.GetSymbols())
} }
return validChars return validChars
} }
func (p *Passage) SetPin(pin string) {
p.pin = pin
}
func (p *Passage) GetPassword(door string) string { func (p *Passage) GetPassword(door string) string {
ret := "" ret := ""
validChars := p.GetValidChars()
hashing := p.pin + " - " + door hashing := p.pin + " - " + door
shaArr := sha256.Sum256([]byte(hashing)) shaArr := sha256.Sum256([]byte(hashing))
var sha []byte var sha []byte
for i := range shaArr { for i := range shaArr {
sha = append(sha, shaArr[i]) sha = append(sha, shaArr[i])
} }
if p.requireEachCharSet {
validChars := p.GetValidCharSets()
for i := 0; i < p.length; i++ {
set := i % len(validChars)
ret += string(validChars[set][int(sha[i])%len(validChars)])
}
} else {
validChars := p.GetValidChars()
for i := 0; i < p.length; i++ { for i := 0; i < p.length; i++ {
ret += string(validChars[int(sha[i])%len(validChars)]) ret += string(validChars[int(sha[i])%len(validChars)])
} }
}
return ret return ret
} }