v0.0.52
This commit is contained in:
parent
64cc1342a0
commit
1a1cd6d0aa
@ -179,6 +179,72 @@ func (ph PassHash) Upgrade(plainpass string) (PassHash, error) {
|
||||
}
|
||||
}
|
||||
|
||||
func (ph PassHash) ClearTOTP() (PassHash, error) {
|
||||
version, _, _, _, _, valid := ph.Data()
|
||||
if !valid {
|
||||
return "", errors.New("invalid PassHash")
|
||||
}
|
||||
|
||||
if version == 0 {
|
||||
return ph, nil
|
||||
}
|
||||
|
||||
if version == 1 {
|
||||
return ph, nil
|
||||
}
|
||||
|
||||
if version == 2 {
|
||||
return ph, nil
|
||||
}
|
||||
|
||||
if version == 3 {
|
||||
split := strings.Split(string(ph), "|")
|
||||
split[3] = "0"
|
||||
return PassHash(strings.Join(split, "|")), nil
|
||||
}
|
||||
|
||||
if version == 4 {
|
||||
split := strings.Split(string(ph), "|")
|
||||
split[2] = "0"
|
||||
return PassHash(strings.Join(split, "|")), nil
|
||||
}
|
||||
|
||||
return "", errors.New("unknown version")
|
||||
}
|
||||
|
||||
func (ph PassHash) WithTOTP(totpSecret []byte) (PassHash, error) {
|
||||
version, _, _, _, _, valid := ph.Data()
|
||||
if !valid {
|
||||
return "", errors.New("invalid PassHash")
|
||||
}
|
||||
|
||||
if version == 0 {
|
||||
return "", errors.New("version does not support totp, needs upgrade")
|
||||
}
|
||||
|
||||
if version == 1 {
|
||||
return "", errors.New("version does not support totp, needs upgrade")
|
||||
}
|
||||
|
||||
if version == 2 {
|
||||
return "", errors.New("version does not support totp, needs upgrade")
|
||||
}
|
||||
|
||||
if version == 3 {
|
||||
split := strings.Split(string(ph), "|")
|
||||
split[3] = hex.EncodeToString(totpSecret)
|
||||
return PassHash(strings.Join(split, "|")), nil
|
||||
}
|
||||
|
||||
if version == 4 {
|
||||
split := strings.Split(string(ph), "|")
|
||||
split[2] = hex.EncodeToString(totpSecret)
|
||||
return PassHash(strings.Join(split, "|")), nil
|
||||
}
|
||||
|
||||
return "", errors.New("unknown version")
|
||||
}
|
||||
|
||||
func (ph PassHash) String() string {
|
||||
return string(ph)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user