From 1dc31051550228023c8ef0e98e42e302079a8f72 Mon Sep 17 00:00:00 2001 From: claudemiro Date: Fri, 16 Jan 2015 13:18:24 -0300 Subject: [PATCH] Moved function to generate sessions Ids from websocket to Utils --- utils/utils.go | 13 +++++++++++++ websockets.go | 15 +-------------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/utils/utils.go b/utils/utils.go index 6834453..3824a06 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -6,7 +6,9 @@ package utils import ( "crypto/hmac" + "crypto/rand" "crypto/sha256" + "encoding/base32" "encoding/hex" ) @@ -18,3 +20,14 @@ func HashMAC(message, key []byte) string { return hex.EncodeToString(expected) } + +// Generate a new random Hash +func RandomHash() string { + b := make([]byte, 25) + + if _, err := rand.Read(b); err != nil { + panic("websockets: Could not generate a random session ID") + } + + return base32.StdEncoding.EncodeToString(b) +} diff --git a/websockets.go b/websockets.go index 8653399..4e9ec79 100644 --- a/websockets.go +++ b/websockets.go @@ -5,8 +5,6 @@ package main import ( - "crypto/rand" - "encoding/base32" "encoding/json" "fmt" "io" @@ -225,7 +223,7 @@ func Websocket(w http.ResponseWriter, r *http.Request) { return } - sessionID := randomSessionID() + sessionID := utils.RandomHash() if err := onOpen(conn, w, r, sessionID, app); err != nil { emitWSError(err, conn) @@ -235,17 +233,6 @@ func Websocket(w http.ResponseWriter, r *http.Request) { onMessage(conn, w, r, sessionID, app) } -// Generate a new random sessionID -func randomSessionID() string { - b := make([]byte, 25) - - if _, err := rand.Read(b); err != nil { - panic("websockets: Could not generate a random session ID") - } - - return base32.StdEncoding.EncodeToString(b) -} - // Emit an Websocket ErrorEvent func emitWSError(err WebsocketError, conn *websocket.Conn) {