forked from madari/go-socket.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession.go
35 lines (27 loc) · 752 Bytes
/
session.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
package socketio
import (
"io"
"crypto/rand"
"os"
)
// SessionID is just a string for now.
type SessionID string
const (
// Length of the session ids.
SessionIDLength = 16
// Charset from which to build the session ids.
SessionIDCharset = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
)
// NewSessionID creates a new ~random session id that is SessionIDLength long and
// consists of random characters from the SessionIDCharset.
func NewSessionID() (sid SessionID, err os.Error) {
b := make([]byte, SessionIDLength)
if _, err = io.ReadFull(rand.Reader, b); err != nil {
return
}
for i := 0; i < SessionIDLength; i++ {
b[i] = SessionIDCharset[b[i]%uint8(len(SessionIDCharset))]
}
sid = SessionID(b)
return
}