-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgroup.go
More file actions
54 lines (45 loc) · 1.27 KB
/
Copy pathgroup.go
File metadata and controls
54 lines (45 loc) · 1.27 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
package srp
import (
"fmt"
"math/big"
"github.com/bodgit/srp/internal/rfc5054"
"github.com/bodgit/srp/internal/util"
)
// Group represents the SRP group parameters.
type Group struct {
G *big.Int
N *big.Int
Size int
}
//nolint:gochecknoglobals
var rfcGroups = map[int]*Group{
1024: util.Must(NewGroup(2, 1024, rfc5054.Hex1024)),
1536: util.Must(NewGroup(2, 1536, rfc5054.Hex1536)),
2048: util.Must(NewGroup(2, 2048, rfc5054.Hex2048)),
3072: util.Must(NewGroup(5, 3072, rfc5054.Hex3072)),
4096: util.Must(NewGroup(5, 4096, rfc5054.Hex4096)),
6144: util.Must(NewGroup(5, 6144, rfc5054.Hex6144)),
8192: util.Must(NewGroup(19, 8192, rfc5054.Hex8192)),
}
// NewGroup returns a Group with the generator g, and a prime of size bits set
// to the bytes decoded from s.
func NewGroup(g int64, size int, s string) (*Group, error) {
b, err := util.BytesFromHexString(s)
if err != nil {
return nil, fmt.Errorf("unable to convert hex string to bytes: %w", err)
}
group := &Group{
G: big.NewInt(g),
N: new(big.Int).SetBytes(b),
Size: size >> 3,
}
return group, nil
}
// GetGroup returns the RFC 5054 group for the prime of n bits.
func GetGroup(n int) (*Group, error) {
group, ok := rfcGroups[n]
if !ok {
return nil, util.ErrGroupNotFound
}
return group, nil
}