-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdecode.go
More file actions
43 lines (35 loc) · 1.2 KB
/
Copy pathdecode.go
File metadata and controls
43 lines (35 loc) · 1.2 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
package crypt
import (
"sync"
"github.com/go-crypt/crypt/algorithm"
)
// The global Decoder. This is utilized by the Decode function. It is initialized exactly once by gdecoderOnce so that
// the Decode function is safe for concurrent use.
var (
gdecoder *Decoder
gdecoderErr error
gdecoderOnce sync.Once
)
// Decode is a convenience function which wraps the Decoder functionality. It's recommended to create your own decoder
// instead via NewDecoder or NewDefaultDecoder.
//
// CRITICAL STABILITY NOTE: the decoders loaded via this function are not guaranteed to remain the same. It is strongly
// recommended that users implementing this library use the NewDecoder function and explicitly register each decoder
// which they wish to support.
func Decode(encodedDigest string) (digest algorithm.Digest, err error) {
if digest, err = decode(encodedDigest); err != nil {
return nil, err
}
return digest, nil
}
func decode(encodedDigest string) (digest algorithm.Digest, err error) {
gdecoderOnce.Do(func() {
if gdecoder, gdecoderErr = NewDefaultDecoder(); gdecoderErr == nil {
gdecoder.global = true
}
})
if gdecoderErr != nil {
return nil, gdecoderErr
}
return gdecoder.Decode(encodedDigest)
}