Home

go-spectre @main - refs - log -
-
https://git.jolheiser.com/go-spectre.git
Go implementation for spectre/masterpassword
go-spectre / spectre.go
- raw
 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
package spectre

import "encoding/binary"

// Spectre is a spectre client
type Spectre struct {
	name   string
	secret string
	scoper Scoper

	key []byte
}

// New returns a Spectre client
func New(name, secret string, opts ...Option) (s *Spectre, err error) {
	s = &Spectre{
		name:   name,
		secret: secret,
		scoper: DefaultScoper,
	}
	for _, opt := range opts {
		opt(s)
	}
	s.key, err = userKey(s.name, s.secret, s.scoper)
	return
}

// Option is a Spectre option
type Option func(*Spectre)

// WithScoper assigns a scoper to Spectre
func WithScoper(scoper Scoper) Option {
	return func(s *Spectre) {
		s.scoper = scoper
	}
}

// Site returns a site password based on Options
func (s *Spectre) Site(siteName string, opts ...SiteOption) string {
	return site(s.key, s.scoper, siteName, opts...)
}

type options struct {
	template Template
	counter  int
	scope    Scope
}

// SiteOption is an option for Spectre.Site
type SiteOption func(*options)

// WithTemplate specifies a Template
func WithTemplate(t Template) SiteOption {
	return func(opts *options) {
		opts.template = t
	}
}

// WithCounter specifies a counter
func WithCounter(c int) SiteOption {
	return func(opts *options) {
		opts.counter = c
	}
}

// WithScope specifies a Scope
func WithScope(s Scope) SiteOption {
	return func(opts *options) {
		opts.scope = s
	}
}

func bigEndian(num int) []byte {
	buf := make([]byte, 4)
	binary.BigEndian.PutUint32(buf, uint32(num))
	return buf
}