-
Notifications
You must be signed in to change notification settings - Fork 1
/
auth.go
49 lines (37 loc) · 974 Bytes
/
auth.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
package radius
import (
"bytes"
"crypto/md5"
"encoding/binary"
)
// An Authenticator is responsible for calculating the Authenticator field of the
// packet
type Authenticator interface {
Calculate(p *Packet) ([]byte, error)
}
// AccountingRequestAuthenticator returns an implementation of Authenticator used for sending RADIUS accounting requests
func AccountingRequestAuthenticator(sharedSecret string) Authenticator {
return &reqAuth{
sharedSecret: sharedSecret,
}
}
type reqAuth struct {
sharedSecret string
}
func (r *reqAuth) Calculate(p *Packet) ([]byte, error) {
output := bytes.NewBuffer([]byte{})
p.Code.Write(output)
p.ID.Write(output)
binary.Write(output, binary.BigEndian, p.Length())
for i := 0; i != 16; i++ {
output.Write([]byte{0})
}
for _, a := range p.Attributes {
if err := a.Write(output); err != nil {
return nil, err
}
}
output.Write([]byte(r.sharedSecret))
ret := md5.Sum(output.Bytes())
return ret[:], nil
}