This repository was archived by the owner on Jan 22, 2024. It is now read-only.
forked from iromli/go-itsdangerous
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsignature.go
More file actions
244 lines (210 loc) · 6.14 KB
/
signature.go
File metadata and controls
244 lines (210 loc) · 6.14 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package itsdangerous
import (
"bytes"
"crypto/hmac"
"crypto/sha1"
"encoding/binary"
"errors"
"fmt"
"hash"
)
// Signature can sign bytes and unsign it and validate the signature
// provided.
//
// Salt can be used to namespace the hash, so that a signed string is only
// valid for a given namespace. Leaving this at the default value or re-using
// a salt value across different parts of your application where the same
// signed value in one part can mean something different in another part
// is a security risk.
type Signature struct {
SecretKey []byte
Sep []byte
Salt []byte
KeyDerivation string
DigestMethod hash.Hash
Algorithm SigningAlgorithm
}
// DeriveKey generates a key derivation. Keep in mind that the key derivation in itsdangerous
// is not intended to be used as a security method to make a complex key out of a short password.
// Instead you should use large random secret keys.
func (s *Signature) DeriveKey() ([]byte, error) {
var key []byte
var err error
s.DigestMethod.Reset()
switch s.KeyDerivation {
case "concat":
h := s.DigestMethod
h.Write(s.Salt)
h.Write(s.SecretKey)
key = h.Sum(nil)
case "django-concat":
h := s.DigestMethod
h.Write(s.Salt)
h.Write([]byte("signer"))
h.Write(s.SecretKey)
key = h.Sum(nil)
case "hmac":
h := hmac.New(func() hash.Hash { return s.DigestMethod }, s.SecretKey)
h.Write(s.Salt)
key = h.Sum(nil)
case "none":
key = s.SecretKey
default:
key, err = nil, errors.New("unknown key derivation method")
}
return key, err
}
// Get returns the signature for the given value.
func (s *Signature) Get(value []byte) ([]byte, error) {
key, err := s.DeriveKey()
if err != nil {
return nil, err
}
sig := s.Algorithm.GetSignature(key, value)
return base64Encode(sig), err
}
// Verify verifies the signature for the given value.
func (s *Signature) Verify(value, sig []byte) (bool, error) {
key, err := s.DeriveKey()
if err != nil {
return false, err
}
signed, err := base64Decode(sig)
if err != nil {
return false, err
}
return s.Algorithm.VerifySignature(key, value, signed), nil
}
// Sign the given string.
func (s *Signature) Sign(value []byte) ([]byte, error) {
sig, err := s.Get(value)
if err != nil {
return nil, err
}
return sepJoin(value, s.Sep, sig), nil
}
// Unsign the given string.
func (s *Signature) Unsign(signed []byte) ([]byte, error) {
if !bytes.Contains(signed, s.Sep) {
return nil, fmt.Errorf("no %s found in value", s.Sep)
}
li := bytes.LastIndex(signed, s.Sep)
value, sig := signed[:li], signed[li+len(s.Sep):]
if ok, _ := s.Verify(value, sig); ok == true {
return value, nil
}
return nil, fmt.Errorf("signature %s does not match", sig)
}
// SignB64 first Base64 encodes the (optionally compressed) value before signing.
// This is compatable with itsdangerous URLSafeSerializer
func (s *Signature) SignB64(value []byte) ([]byte, error) {
return s.Sign(ZBase64Encode(value))
}
// UnsignB64 Base64 decodes the (optionally compressed) value after unsigning
// This is compatable with itsdangerous URLSafeSerializer
func (s *Signature) UnsignB64(signed []byte) ([]byte, error) {
b, err := s.Unsign(signed)
if err != nil {
return nil, err
}
return base64Decode(b)
}
// NewSignature creates a new Signature
func NewSignature(secret, salt, sep, derivation string, digest hash.Hash, algo SigningAlgorithm) *Signature {
if salt == "" {
salt = "itsdangerous.Signer"
}
if sep == "" {
sep = "."
}
if derivation == "" {
derivation = "django-concat"
}
if digest == nil {
digest = sha1.New()
}
if algo == nil {
algo = &HMACAlgorithm{DigestMethod: digest}
}
return &Signature{
SecretKey: []byte(secret),
Salt: []byte(salt),
Sep: []byte(sep),
KeyDerivation: derivation,
DigestMethod: digest,
Algorithm: algo,
}
}
// TimestampSignature works like the regular Signature but also records the time
// of the signing and can be used to expire signatures.
type TimestampSignature struct {
Signature
}
// Sign the given string.
func (s *TimestampSignature) Sign(value []byte) ([]byte, error) {
buf := new(bytes.Buffer)
if err := binary.Write(buf, binary.BigEndian, getTimestamp()); err != nil {
return nil, err
}
ts := base64Encode(buf.Bytes())
val := sepJoin(value, s.Sep, ts)
sig, err := s.Get(val)
if err != nil {
return nil, err
}
return sepJoin(val, s.Sep, sig), nil
}
// SignB64 first Base64 encodes the (optionally compressed) value before signing.
// This is compatable with itsdangerous URLSafeTimedSerializer
func (s *TimestampSignature) SignB64(value []byte) ([]byte, error) {
return s.Sign(ZBase64Encode(value))
}
// UnsignB64 Base64 decodes the (optionally compressed) value after unsigning
// This is compatable with itsdangerous URLSafeTimedSerializer
func (s *TimestampSignature) UnsignB64(signed []byte, maxAge uint32) ([]byte, error) {
b, err := s.Unsign(signed, maxAge)
if err != nil {
return nil, err
}
return base64Decode(b)
}
// Unsign the given string.
func (s *TimestampSignature) Unsign(value []byte, maxAge uint32) ([]byte, error) {
var timestamp uint32
result, err := s.Signature.Unsign(value)
if err != nil {
return nil, err
}
// If there is no timestamp in the result there is something seriously wrong.
if !bytes.Contains(result, s.Sep) {
return nil, errors.New("timestamp missing")
}
li := bytes.LastIndex(result, s.Sep)
val, ts := result[:li], result[li+len(s.Sep):]
sig, err := base64Decode(ts)
if err != nil {
return nil, err
}
buf := bytes.NewReader(sig)
if err = binary.Read(buf, binary.BigEndian, ×tamp); err != nil {
return nil, err
}
if maxAge > 0 {
if age := getTimestamp() - timestamp; age > maxAge {
return nil, fmt.Errorf("signature age %d > %d seconds", age, maxAge)
}
}
return val, nil
}
// NewTimestampSignature creates a new TimestampSignature
func NewTimestampSignature(secret, salt, sep, derivation string, digest hash.Hash, algo SigningAlgorithm) *TimestampSignature {
s := NewSignature(secret, salt, sep, derivation, digest, algo)
return &TimestampSignature{Signature: *s}
}
func sepJoin(value, sep, sig []byte) []byte {
buf := bytes.Buffer{}
buf.Write(value)
buf.Write(sep)
buf.Write(sig)
return buf.Bytes()
}