lessmore/node/helpers.go

64 lines
1.3 KiB
Go
Raw Permalink Normal View History

2018-11-05 18:24:46 +03:00
package node
import (
"time"
mrand "math/rand"
2018-11-06 08:41:04 +03:00
"strings"
2018-11-12 15:06:39 +03:00
log "github.com/sirupsen/logrus"
2018-11-05 18:24:46 +03:00
"golang.org/x/crypto/bcrypt"
)
func hashAndSalt(authString []byte) string {
hash, err := bcrypt.GenerateFromPassword(authString, bcrypt.MinCost)
if err != nil {
log.Fatal(err.Error())
}
return string(hash)
}
// genAuthString random auth string
func genAuthString() []byte {
authString := make([]byte, AUTH_STRING_LENGTH)
mrand.Seed(time.Now().UnixNano())
for i := range authString {
authString[i] = LETTERS[mrand.Intn(len(LETTERS))]
}
return authString
}
2018-11-06 08:41:04 +03:00
// parsePointBody without regexp
// it dirty hack mades because *http.Request.Form not parsed it
func parsePointBody(content string) (string, string) {
var pauth, tmsg string
log.Debug("Body parser: ", content)
if strings.Contains(content, "&") {
log.Debug("Found &")
for _, v := range strings.Split(content, "&") {
if strings.Contains(v, "pauth") {
log.Debug("Found pauth")
pauth = strings.Split(v, "pauth=")[1]
}
if strings.Contains(v, "tmsg") {
log.Debug("Found tmsg")
tmsg = strings.Split(v, "tmsg=")[1]
}
}
}
return pauth, tmsg
}
2018-11-06 13:57:27 +03:00
// b64replace +,/,-,_ with A and Z
func b64replace(s string) string {
s = strings.Replace(s, "+", "A", -1)
s = strings.Replace(s, "-", "A", -1)
s = strings.Replace(s, "/", "Z", -1)
s = strings.Replace(s, "_", "Z", -1)
return s
}