mirror of
https://github.com/miniflux/v2.git
synced 2024-08-15 00:43:24 +02:00
A new "shareCode" field is generated for each entry, and allows unlogged users to access the entry through the /shared endpoint. This feature is particularly useful to share articles from miniflux to third-party users without having them to visit the original source. The image proxy is disabled and special cache headers are proposed in the shared page to avoid denial of service.
45 lines
1.1 KiB
Go
45 lines
1.1 KiB
Go
// 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 "miniflux.app/crypto"
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"crypto/sha256"
|
|
"encoding/base64"
|
|
"encoding/hex"
|
|
"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))
|
|
}
|
|
|
|
// GenerateRandomStringHex returns a random hexadecimal string.
|
|
func GenerateRandomStringHex(size int) string {
|
|
return hex.EncodeToString(GenerateRandomBytes(size))
|
|
}
|