2019-05-28 01:16:50 +00:00
|
|
|
package crypto
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func CombinePasswordAndKey(pass, key []byte) ([]byte, error) {
|
|
|
|
if len(pass) < 8 {
|
|
|
|
return nil, fmt.Errorf("master password must be at least 8 characters")
|
|
|
|
}
|
|
|
|
if len(key) != 16 {
|
|
|
|
return nil, fmt.Errorf("key was not of length 16")
|
|
|
|
}
|
|
|
|
|
|
|
|
for idx := 0; len(pass) < 16; idx++ {
|
|
|
|
pass = append(pass, pass[idx])
|
|
|
|
}
|
|
|
|
|
|
|
|
return append(pass[:16], key...), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func GeneratePassword(length int, numbers, specials bool) string {
|
2019-07-18 02:27:03 +00:00
|
|
|
const alphaValues = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
|
|
const numberValues = "1234567890"
|
|
|
|
const specialValues = "!@#$%^&*()_-+="
|
2019-05-28 01:16:50 +00:00
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
pass := make([]byte, length)
|
|
|
|
|
2019-07-18 02:27:03 +00:00
|
|
|
values := alphaValues
|
|
|
|
|
2019-05-28 01:16:50 +00:00
|
|
|
switch {
|
|
|
|
case numbers && specials:
|
2019-07-18 02:27:03 +00:00
|
|
|
values += numberValues + specialValues
|
2019-05-28 01:16:50 +00:00
|
|
|
case numbers:
|
2019-07-18 02:27:03 +00:00
|
|
|
values += numberValues
|
2019-05-28 01:16:50 +00:00
|
|
|
case specials:
|
2019-07-18 02:27:03 +00:00
|
|
|
values += specialValues
|
|
|
|
}
|
|
|
|
|
|
|
|
for idx := range pass {
|
|
|
|
pass[idx] = values[rand.Int63()%int64(len(values))]
|
2019-05-28 01:16:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return string(pass)
|
|
|
|
}
|