File: unlock.go

package info (click to toggle)
golang-github-henrybear327-go-proton-api 1.0.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,088 kB
  • sloc: sh: 55; makefile: 26
file content (41 lines) | stat: -rw-r--r-- 1,100 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package proton

import (
	"fmt"
	"runtime"

	"github.com/ProtonMail/gluon/async"
	"github.com/ProtonMail/gopenpgp/v2/crypto"
	"github.com/bradenaw/juniper/parallel"
)

func Unlock(user User, addresses []Address, saltedKeyPass []byte, panicHandler async.PanicHandler) (*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 {
		defer async.HandlePanic(panicHandler)

		return addr.Keys.TryUnlock(saltedKeyPass, userKR)
	}) {
		if addrKR == nil {
			continue
		} else if addrKR.CountDecryptionEntities() == 0 {
			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
}