grafana/pkg/util/encryption.go

69 lines
1.8 KiB
Go
Raw Normal View History

2016-01-22 13:15:39 -06:00
package util
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
2016-01-25 15:15:29 -06:00
"crypto/sha256"
"errors"
2016-01-22 13:15:39 -06:00
"io"
)
2016-01-25 15:15:29 -06:00
const saltLength = 8
pkg/util/*: Add missing function comments. See, $ gometalinter --vendor --deadline 10m --disable-all --enable=golint ./... encoding.go:15:1:warning: comment on exported function GetRandomString should be of the form "GetRandomString ..." (golint) encoding.go:30:1:warning: exported function EncodePassword should have comment or be unexported (golint) encoding.go:35:1:warning: comment on exported function EncodeMd5 should be of the form "EncodeMd5 ..." (golint) encoding.go:42:1:warning: comment on exported function PBKDF2 should be of the form "PBKDF2 ..." (golint) encoding.go:80:1:warning: exported function GetBasicAuthHeader should have comment or be unexported (golint) encoding.go:85:1:warning: exported function DecodeBasicAuthHeader should have comment or be unexported (golint) encoding.go:105:1:warning: exported function RandomHex should have comment or be unexported (golint) encryption.go:14:1:warning: exported function Decrypt should have comment or be unexported (golint) encryption.go:39:1:warning: exported function Encrypt should have comment or be unexported (golint) ip.go:7:1:warning: exported function SplitIpPort should have comment or be unexported (golint) json.go:3:6:warning: exported type DynMap should have comment or be unexported (golint) md5.go:22:1:warning: comment on exported function Md5SumString should be of the form "Md5SumString ..." (golint) strings.go:10:1:warning: exported function StringsFallback2 should have comment or be unexported (golint) strings.go:14:1:warning: exported function StringsFallback3 should have comment or be unexported (golint) strings.go:27:1:warning: exported function SplitString should have comment or be unexported (golint) strings.go:35:1:warning: exported function GetAgeString should have comment or be unexported (golint) url.go:8:6:warning: exported type UrlQueryReader should have comment or be unexported (golint) url.go:12:1:warning: exported function NewUrlQueryReader should have comment or be unexported (golint) url.go:23:1:warning: exported method UrlQueryReader.Get should have comment or be unexported (golint) url.go:32:1:warning: exported function JoinUrlFragments should have comment or be unexported (golint) validation.go:16:1:warning: exported function IsEmail should have comment or be unexported (golint)
2019-01-28 15:09:40 -06:00
// Decrypt decrypts a payload with a given secret.
func Decrypt(payload []byte, secret string) ([]byte, error) {
2016-01-25 15:15:29 -06:00
salt := payload[:saltLength]
key := encryptionKeyToBytes(secret, string(salt))
2016-01-22 13:15:39 -06:00
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
2016-01-22 13:15:39 -06:00
}
// The IV needs to be unique, but not secure. Therefore it's common to
// include it at the beginning of the ciphertext.
if len(payload) < aes.BlockSize {
return nil, errors.New("payload too short")
2016-01-22 13:15:39 -06:00
}
2016-01-25 15:15:29 -06:00
iv := payload[saltLength : saltLength+aes.BlockSize]
payload = payload[saltLength+aes.BlockSize:]
payloadDst := make([]byte, len(payload))
2016-01-22 13:15:39 -06:00
stream := cipher.NewCFBDecrypter(block, iv)
// XORKeyStream can work in-place if the two arguments are the same.
stream.XORKeyStream(payloadDst, payload)
return payloadDst, nil
2016-01-22 13:15:39 -06:00
}
pkg/util/*: Add missing function comments. See, $ gometalinter --vendor --deadline 10m --disable-all --enable=golint ./... encoding.go:15:1:warning: comment on exported function GetRandomString should be of the form "GetRandomString ..." (golint) encoding.go:30:1:warning: exported function EncodePassword should have comment or be unexported (golint) encoding.go:35:1:warning: comment on exported function EncodeMd5 should be of the form "EncodeMd5 ..." (golint) encoding.go:42:1:warning: comment on exported function PBKDF2 should be of the form "PBKDF2 ..." (golint) encoding.go:80:1:warning: exported function GetBasicAuthHeader should have comment or be unexported (golint) encoding.go:85:1:warning: exported function DecodeBasicAuthHeader should have comment or be unexported (golint) encoding.go:105:1:warning: exported function RandomHex should have comment or be unexported (golint) encryption.go:14:1:warning: exported function Decrypt should have comment or be unexported (golint) encryption.go:39:1:warning: exported function Encrypt should have comment or be unexported (golint) ip.go:7:1:warning: exported function SplitIpPort should have comment or be unexported (golint) json.go:3:6:warning: exported type DynMap should have comment or be unexported (golint) md5.go:22:1:warning: comment on exported function Md5SumString should be of the form "Md5SumString ..." (golint) strings.go:10:1:warning: exported function StringsFallback2 should have comment or be unexported (golint) strings.go:14:1:warning: exported function StringsFallback3 should have comment or be unexported (golint) strings.go:27:1:warning: exported function SplitString should have comment or be unexported (golint) strings.go:35:1:warning: exported function GetAgeString should have comment or be unexported (golint) url.go:8:6:warning: exported type UrlQueryReader should have comment or be unexported (golint) url.go:12:1:warning: exported function NewUrlQueryReader should have comment or be unexported (golint) url.go:23:1:warning: exported method UrlQueryReader.Get should have comment or be unexported (golint) url.go:32:1:warning: exported function JoinUrlFragments should have comment or be unexported (golint) validation.go:16:1:warning: exported function IsEmail should have comment or be unexported (golint)
2019-01-28 15:09:40 -06:00
// Encrypt encrypts a payload with a given secret.
func Encrypt(payload []byte, secret string) ([]byte, error) {
2016-01-25 15:15:29 -06:00
salt := GetRandomString(saltLength)
2016-01-22 13:15:39 -06:00
2016-01-25 15:15:29 -06:00
key := encryptionKeyToBytes(secret, salt)
2016-01-22 13:15:39 -06:00
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
2016-01-22 13:15:39 -06:00
}
// The IV needs to be unique, but not secure. Therefore it's common to
// include it at the beginning of the ciphertext.
2016-01-25 15:15:29 -06:00
ciphertext := make([]byte, saltLength+aes.BlockSize+len(payload))
copy(ciphertext[:saltLength], []byte(salt))
iv := ciphertext[saltLength : saltLength+aes.BlockSize]
2016-01-22 13:15:39 -06:00
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
return nil, err
2016-01-22 13:15:39 -06:00
}
stream := cipher.NewCFBEncrypter(block, iv)
2016-01-25 15:15:29 -06:00
stream.XORKeyStream(ciphertext[saltLength+aes.BlockSize:], payload)
2016-01-22 13:15:39 -06:00
return ciphertext, nil
2016-01-22 13:15:39 -06:00
}
// Key needs to be 32bytes
2016-01-25 15:15:29 -06:00
func encryptionKeyToBytes(secret, salt string) []byte {
return PBKDF2([]byte(secret), []byte(salt), 10000, 32, sha256.New)
2016-01-22 13:15:39 -06:00
}