-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcertificate.go
61 lines (53 loc) · 1.43 KB
/
certificate.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
50
51
52
53
54
55
56
57
58
59
60
61
package go_i2cp
const (
CERTIFICATE_NULL uint8 = iota
CERTIFICATE_HASHCASH uint8 = iota
CERTIFICATE_SIGNED uint8 = iota
CERTIFICATE_MULTIPLE uint8 = iota
)
type Certificate struct {
certType uint8
data []byte
length uint16
}
func NewCertificate(typ uint8) (cert Certificate) {
cert.certType = typ
return
}
func NewCertificateFromMessage(stream *Stream) (cert Certificate, err error) {
cert.certType, err = stream.ReadByte()
cert.length, err = stream.ReadUint16()
if err != nil {
return
}
if (cert.certType != CERTIFICATE_NULL) && (cert.length != 0) {
Fatal(CERTIFICATE|PROTOCOL, "Only null certificates are allowed to have zero length.")
return
} else if cert.certType == CERTIFICATE_NULL {
return
}
cert.data = make([]byte, cert.length)
_, err = stream.Read(cert.data)
return
}
func NewCertificateFromStream(stream *Stream) (Certificate, error) {
return NewCertificateFromMessage(stream)
}
func (cert *Certificate) Copy() (newCert Certificate) {
newCert.certType = cert.certType
newCert.length = cert.length
newCert.data = make([]byte, len(cert.data))
copy(newCert.data, cert.data)
return
}
func (cert *Certificate) WriteToMessage(stream *Stream) (err error) {
err = stream.WriteByte(cert.certType)
err = stream.WriteUint16(cert.length)
if cert.length > 0 {
_, err = stream.Write(cert.data)
}
return
}
func (cert *Certificate) WriteToStream(stream *Stream) error {
return cert.WriteToMessage(stream)
}