Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
nexpie-grafana-theme
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Registry
Registry
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kornkitt Poolsup
nexpie-grafana-theme
Commits
ab3b5868
Commit
ab3b5868
authored
Jan 23, 2016
by
Anthony Woods
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add encryption util functions
parent
f94599cd
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
98 additions
and
0 deletions
+98
-0
pkg/util/encryption.go
+70
-0
pkg/util/encryption_test.go
+28
-0
No files found.
pkg/util/encryption.go
0 → 100644
View file @
ab3b5868
package
util
import
(
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"io"
"github.com/grafana/grafana/pkg/log"
)
func
Decrypt
(
payload
[]
byte
,
secret
string
)
[]
byte
{
key
:=
encryptionKeyToBytes
(
secret
)
block
,
err
:=
aes
.
NewCipher
(
key
)
if
err
!=
nil
{
log
.
Fatal
(
4
,
err
.
Error
())
}
// 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
{
log
.
Fatal
(
4
,
"payload too short"
)
}
iv
:=
payload
[
:
aes
.
BlockSize
]
payload
=
payload
[
aes
.
BlockSize
:
]
stream
:=
cipher
.
NewCFBDecrypter
(
block
,
iv
)
// XORKeyStream can work in-place if the two arguments are the same.
stream
.
XORKeyStream
(
payload
,
payload
)
return
payload
}
func
Encrypt
(
payload
[]
byte
,
secret
string
)
[]
byte
{
key
:=
encryptionKeyToBytes
(
secret
)
block
,
err
:=
aes
.
NewCipher
(
key
)
if
err
!=
nil
{
log
.
Fatal
(
4
,
err
.
Error
())
}
// The IV needs to be unique, but not secure. Therefore it's common to
// include it at the beginning of the ciphertext.
ciphertext
:=
make
([]
byte
,
aes
.
BlockSize
+
len
(
payload
))
iv
:=
ciphertext
[
:
aes
.
BlockSize
]
if
_
,
err
:=
io
.
ReadFull
(
rand
.
Reader
,
iv
);
err
!=
nil
{
log
.
Fatal
(
4
,
err
.
Error
())
}
stream
:=
cipher
.
NewCFBEncrypter
(
block
,
iv
)
stream
.
XORKeyStream
(
ciphertext
[
aes
.
BlockSize
:
],
payload
)
return
ciphertext
}
// Key needs to be 32bytes
func
encryptionKeyToBytes
(
secret
string
)
[]
byte
{
key
:=
make
([]
byte
,
32
,
32
)
keyBytes
:=
[]
byte
(
secret
)
secretLength
:=
len
(
keyBytes
)
for
i
:=
0
;
i
<
32
;
i
++
{
if
secretLength
>
i
{
key
[
i
]
=
keyBytes
[
i
]
}
else
{
key
[
i
]
=
0
}
}
return
key
}
pkg/util/encryption_test.go
0 → 100644
View file @
ab3b5868
package
util
import
(
"testing"
.
"github.com/smartystreets/goconvey/convey"
)
func
TestEncryption
(
t
*
testing
.
T
)
{
Convey
(
"When getting encryption key"
,
t
,
func
()
{
key
:=
encryptionKeyToBytes
(
"secret"
)
So
(
len
(
key
),
ShouldEqual
,
32
)
key
=
encryptionKeyToBytes
(
"a very long secret key that is larger then 32bytes"
)
So
(
len
(
key
),
ShouldEqual
,
32
)
})
Convey
(
"When decrypting basic payload"
,
t
,
func
()
{
encrypted
:=
Encrypt
([]
byte
(
"grafana"
),
"1234"
)
decrypted
:=
Decrypt
(
encrypted
,
"1234"
)
So
(
string
(
decrypted
),
ShouldEqual
,
"grafana"
)
})
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment