From 1a1cd6d0aa866e61448619663e5ac6baf993325e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Schw=C3=B6rer?= Date: Sat, 24 Dec 2022 02:50:46 +0100 Subject: [PATCH] v0.0.52 --- cryptext/passHash.go | 66 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/cryptext/passHash.go b/cryptext/passHash.go index 015076c..673c74d 100644 --- a/cryptext/passHash.go +++ b/cryptext/passHash.go @@ -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) }