forked from keybase/client
/
passphrase_stream_cache.go
91 lines (78 loc) · 1.84 KB
/
passphrase_stream_cache.go
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright 2015 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package libkb
import (
"fmt"
)
type PassphraseStreamCache struct {
tsec Triplesec
passphraseStream *PassphraseStream
}
type PassphraseStreamCacheReader interface {
Triplesec() Triplesec
PassphraseStream() *PassphraseStream
Valid() bool
}
func NewPassphraseStreamCache(tsec Triplesec, ps *PassphraseStream) *PassphraseStreamCache {
return &PassphraseStreamCache{
tsec: tsec,
passphraseStream: ps,
}
}
func (s *PassphraseStreamCache) Triplesec() Triplesec {
if s == nil {
return nil
}
return s.tsec
}
// PassphraseStream returns a copy of the currently cached passphrase stream,
// or nil if none exists.
func (s *PassphraseStreamCache) PassphraseStream() *PassphraseStream {
if s == nil {
return nil
}
return s.passphraseStream.Clone()
}
// PassphraseStream returns a reference to the currently cached passphrase stream,
// or nil if none exists.
func (s *PassphraseStreamCache) PassphraseStreamRef() *PassphraseStream {
if s == nil {
return nil
}
return s.passphraseStream
}
func (s *PassphraseStreamCache) Valid() bool {
return s.ValidPassphraseStream() && s.ValidTsec()
}
func (s *PassphraseStreamCache) ValidPassphraseStream() bool {
if s == nil {
return false
}
return s.passphraseStream != nil
}
func (s *PassphraseStreamCache) ValidTsec() bool {
if s == nil {
return false
}
return s.tsec != nil
}
func (s *PassphraseStreamCache) Clear() {
if s == nil {
return
}
if s.tsec != nil {
s.tsec.Scrub()
s.tsec = nil
}
s.passphraseStream = nil
}
func (s *PassphraseStreamCache) Dump() {
fmt.Printf("PassphraseStreamCache:\n")
if s == nil {
fmt.Printf("nil\n")
return
}
fmt.Printf("PassphraseStreamCache:\n")
fmt.Printf("Valid: %v\n", s.Valid())
fmt.Printf("\n")
}