forked from googlearchive/pubsubbeat
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/logrhythm/pubsubbeat
- Loading branch information
Showing
10 changed files
with
446 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") | ||
|
||
go_library( | ||
name = "go_default_library", | ||
srcs = [ | ||
"decryption.go", | ||
"encryption.go", | ||
], | ||
importpath = "github.com/logrhythm/siem/internal/pkg/crypto", | ||
visibility = ["//:__subpackages__"], | ||
) | ||
|
||
go_test( | ||
name = "go_default_test", | ||
srcs = [ | ||
"decryption_test.go", | ||
"encryption_test.go", | ||
], | ||
embed = [":go_default_library"], | ||
deps = ["@com_github_stretchr_testify//assert:go_default_library"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package crypto | ||
|
||
import ( | ||
"crypto/aes" | ||
"crypto/cipher" | ||
"encoding/base64" | ||
"errors" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
// Decrypt function is used to decrypt the string | ||
func Decrypt(securemess string) (decodedmess string, err error) { | ||
if len(strings.TrimSpace(securemess)) == 0 { | ||
return "", errors.New("string is empty") | ||
} | ||
decodedStr := strings.Split(securemess, "||") | ||
if len(decodedStr) == 2 { | ||
ver, err := strconv.Atoi(decodedStr[0]) | ||
if err != nil { | ||
return "", err | ||
} | ||
switch ver { | ||
case encV1: | ||
decodedmess, err = decrypt1(decodedStr[1]) | ||
if err != nil { | ||
return "", err | ||
} | ||
default: | ||
return "", errors.New("invalid encryption") | ||
} | ||
} | ||
|
||
return decodedmess, nil | ||
} | ||
|
||
func decrypt1(securemess string) (string, error) { | ||
cipherText, err := base64.URLEncoding.DecodeString(securemess) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
block, err := aes.NewCipher(cipherKey) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
if len(cipherText) < aes.BlockSize { | ||
err = errors.New("ciphertext block size is too short") | ||
return "", err | ||
} | ||
|
||
//IV needs to be unique, but doesn't have to be secure. | ||
//It's common to put it at the beginning of the ciphertext. | ||
iv := cipherText[:aes.BlockSize] | ||
cipherText = cipherText[aes.BlockSize:] | ||
|
||
stream := cipher.NewCFBDecrypter(block, iv) | ||
// XORKeyStream can work in-place if the two arguments are the same. | ||
stream.XORKeyStream(cipherText, cipherText) | ||
|
||
decodedmess := string(cipherText) | ||
return decodedmess, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package crypto | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
const TestString = "encryptme" | ||
|
||
func TestDecrypt(t *testing.T) { | ||
t.Run("success decryption", func(t *testing.T) { | ||
enryptedMess, err := Encrypt(TestString) | ||
assert.Nil(t, err) | ||
actual, err := Decrypt(enryptedMess) | ||
assert.Nil(t, err) | ||
assert.Equal(t, TestString, actual) | ||
}) | ||
t.Run("failure decryption", func(t *testing.T) { | ||
str := fmt.Sprintf("%d%s%s", encV1, "||", TestString) | ||
_, err := Decrypt(str) | ||
assert.NotNil(t, err) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package crypto | ||
|
||
import ( | ||
"crypto/aes" | ||
"crypto/cipher" | ||
"crypto/rand" | ||
"encoding/base64" | ||
"errors" | ||
"io" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
var cipherKey = []byte("0123456789012345") | ||
|
||
const ( | ||
encV1 = 1 | ||
) | ||
|
||
//Encrypt function is used to encrypt the string | ||
func Encrypt(message string) (encmess string, err error) { | ||
if len(strings.TrimSpace(message)) == 0 { | ||
return "", errors.New("string is empty") | ||
} | ||
plainText := []byte(message) | ||
|
||
block, err := aes.NewCipher(cipherKey) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
//IV needs to be unique, but doesn't have to be secure. | ||
//It's common to put it at the beginning of the ciphertext. | ||
cipherText := make([]byte, aes.BlockSize+len(plainText)) | ||
iv := cipherText[:aes.BlockSize] | ||
if _, err = io.ReadFull(rand.Reader, iv); err != nil { | ||
return "", err | ||
} | ||
|
||
stream := cipher.NewCFBEncrypter(block, iv) | ||
stream.XORKeyStream(cipherText[aes.BlockSize:], plainText) | ||
|
||
//returns to base64 encoded string | ||
encmess = base64.URLEncoding.EncodeToString(cipherText) | ||
finalEnc := strconv.Itoa(encV1) + "||" + encmess //fmt.Sprintf("%d%s%s", encV1, "||", encmess) | ||
return finalEnc, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package crypto | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestEncrypt(t *testing.T) { | ||
t.Run("success encryption", func(t *testing.T) { | ||
enryptedMess, err := Encrypt(TestString) | ||
assert.Nil(t, err) | ||
_, err = Decrypt(enryptedMess) | ||
assert.Nil(t, err) | ||
}) | ||
} |
Oops, something went wrong.