Passage Library Works

This commit is contained in:
Brian Buller 2017-01-25 11:58:37 -06:00
parent 3b416e625b
commit f37377dd63
3 changed files with 88 additions and 0 deletions

1
gopass-cmd/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
gopass-cmd

18
gopass-cmd/main.go Normal file
View File

@ -0,0 +1,18 @@
package main
import (
"fmt"
"os"
passage "gogs.bullercodeworks.com/brian/passage-go"
)
func main() {
if len(os.Args) < 3 {
fmt.Println("Usage: gopass <pin> <door>")
os.Exit(1)
}
p := passage.CreatePassage(os.Args[1])
fmt.Println("Checksum: " + p.GetChecksumAsString())
fmt.Println("Password: " + p.GetPassword(os.Args[2]))
}

69
passage.go Normal file
View File

@ -0,0 +1,69 @@
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())
}