Skip to content

Instantly share code, notes, and snippets.

@Zenithar
Last active May 14, 2019 19:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Zenithar/e8f19378d108605d4592c597a1c24121 to your computer and use it in GitHub Desktop.
Save Zenithar/e8f19378d108605d4592c597a1c24121 to your computer and use it in GitHub Desktop.
How I write microservice (part 2)
package helpers
import (
"github.com/dchest/uniuri"
validation "github.com/go-ozzo/ozzo-validation"
"github.com/go-ozzo/ozzo-validation/is"
)
// IDGeneratedLength defines the length of the id string
const IDGeneratedLength = 32
// IDGeneratorFunc returns a randomly generated string useable as identifier
var IDGeneratorFunc = func() string {
return uniuri.NewLen(IDGeneratedLength)
}
// IDValidationRules describes identifier contract for syntaxic validation
var IDValidationRules = []validation.Rule{
validation.Required,
validation.Length(IDGeneratedLength, IDGeneratedLength),
is.Alphanumeric,
}
package helpers
import (
"encoding/base64"
"golang.org/x/crypto/blake2b"
)
var principalHashKey = []byte(`7EcP%Sm5=Wgoce5Sb"%[E.<&xG8t5soYU$CzdIMTgK@^4i(Zo|)LoDB'!g"R2]8$`)
// PrincipalHashFunc return the principal hashed using Blake2b keyed algorithm
var PrincipalHashFunc = func(principal string) string {
// Prepare hasher
hasher, err:= blake2b.New512(principalHashKey)
if err!= nil {
panic(err)
}
// Append principal
_, err = hasher.Write([]byte(principal))
if err!= nil {
panic(err)
}
// Return base64 hash value of the principal hash
return base64.RawStdEncoding.EncodeToString(hasher.Sum(nil))
}
// SetPrincipalHashKey used to set the key of hash function
func SetPrincipalHashKey(key []byte) {
if len(key)!= 64 {
panic("Principal hash key length must be 64bytes long.")
}
principalHashKey = key
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment