passage-go/passage.go

70 lines
1.2 KiB
Go

package passage
import (
"crypto/sha256"
"strconv"
)
const (
UseLower = 1 << iota
UseUpper
UseNumber
UseSymbol
)
type Passage struct {
valids int
length int
pin string
}
func CreatePassage(pin string) *Passage {
p := new(Passage)
p.length = 20
p.valids = UseLower | UseUpper | UseNumber | UseSymbol
p.pin = pin
return p
}
func (p *Passage) SetPin(pin string) {
p.pin = pin
}
func (p *Passage) GetPassword(door string) string {
ret := ""
validChars := ""
if p.valids&UseLower == UseLower {
validChars += "abcdefghijklmnopqrstuvwxyz"
}
if p.valids&UseUpper == UseUpper {
validChars += "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
}
if p.valids&UseNumber == UseNumber {
validChars += "1234567890"
}
hashing := p.pin + " - " + door
shaArr := sha256.Sum256([]byte(hashing))
var sha []byte
for i := range shaArr {
sha = append(sha, shaArr[i])
}
for i := 0; i < p.length; i++ {
//for i := range sha {
ret += string(validChars[int(sha[i])%len(validChars)])
}
return ret
}
func (p *Passage) GetChecksum() int {
ret := 0
sha := sha256.Sum256([]byte(p.pin))
for i := range sha {
ret += int(sha[i])
}
return ret
}
func (p *Passage) GetChecksumAsString() string {
return strconv.Itoa(p.GetChecksum())
}