diff options
author | Frédéric Guillot <fred@miniflux.net> | 2018-01-02 19:15:08 -0800 |
---|---|---|
committer | Frédéric Guillot <fred@miniflux.net> | 2018-01-02 19:15:08 -0800 |
commit | c39f2e1a8d2de6d412bcc673d29eb0f7a2d1f5f7 (patch) | |
tree | 950efc2155037ce055e1aae334e160812b0ee38f /crypto | |
parent | 3c3f397bf57e232201fccd64bad2820e6ade567a (diff) |
Rename helper packages
Diffstat (limited to 'crypto')
-rw-r--r-- | crypto/crypto.go | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/crypto/crypto.go b/crypto/crypto.go new file mode 100644 index 0000000..ea8ea2b --- /dev/null +++ b/crypto/crypto.go @@ -0,0 +1,38 @@ +// Copyright 2018 Frédéric Guillot. All rights reserved. +// Use of this source code is governed by the Apache 2.0 +// license that can be found in the LICENSE file. + +package crypto + +import ( + "crypto/rand" + "crypto/sha256" + "encoding/base64" + "fmt" +) + +// HashFromBytes returns a SHA-256 checksum of the input. +func HashFromBytes(value []byte) string { + sum := sha256.Sum256(value) + return fmt.Sprintf("%x", sum) +} + +// Hash returns a SHA-256 checksum of a string. +func Hash(value string) string { + return HashFromBytes([]byte(value)) +} + +// GenerateRandomBytes returns random bytes. +func GenerateRandomBytes(size int) []byte { + b := make([]byte, size) + if _, err := rand.Read(b); err != nil { + panic(err) + } + + return b +} + +// GenerateRandomString returns a random string. +func GenerateRandomString(size int) string { + return base64.URLEncoding.EncodeToString(GenerateRandomBytes(size)) +} |