mirror of
https://github.com/ProtonMail/go-proton-api.git
synced 2025-12-23 23:57:50 -05:00
39 lines
989 B
Go
39 lines
989 B
Go
package proton
|
|
|
|
import (
|
|
"fmt"
|
|
"runtime"
|
|
|
|
"github.com/ProtonMail/gopenpgp/v2/crypto"
|
|
"github.com/bradenaw/juniper/parallel"
|
|
)
|
|
|
|
func Unlock(user User, addresses []Address, saltedKeyPass []byte) (*crypto.KeyRing, map[string]*crypto.KeyRing, error) {
|
|
userKR, err := user.Keys.Unlock(saltedKeyPass, nil)
|
|
if err != nil {
|
|
return nil, nil, fmt.Errorf("failed to unlock user keys: %w", err)
|
|
} else if userKR.CountDecryptionEntities() == 0 {
|
|
return nil, nil, fmt.Errorf("failed to unlock any user keys")
|
|
}
|
|
|
|
addrKRs := make(map[string]*crypto.KeyRing)
|
|
|
|
for idx, addrKR := range parallel.Map(runtime.NumCPU(), addresses, func(addr Address) *crypto.KeyRing {
|
|
return addr.Keys.TryUnlock(saltedKeyPass, userKR)
|
|
}) {
|
|
if addrKR.CountDecryptionEntities() == 0 {
|
|
continue
|
|
} else if addrKR == nil {
|
|
continue
|
|
}
|
|
|
|
addrKRs[addresses[idx].ID] = addrKR
|
|
}
|
|
|
|
if len(addrKRs) == 0 {
|
|
return nil, nil, fmt.Errorf("failed to unlock any address keys")
|
|
}
|
|
|
|
return userKR, addrKRs, nil
|
|
}
|