Commit 038f6034 authored by Sandeep S. Shewalkar's avatar Sandeep S. Shewalkar
Browse files

Security helper and compression helper added

parent 221f5df7
Branches
Tags
No related merge requests found
Showing with 89 additions and 9 deletions
package coreos
import "github.com/DataDog/zstd"
//Compress Compress
func Compress(inputData []byte) ([]byte, error) {
compressedData, err := zstd.Compress(nil, inputData)
return compressedData, err
}
// Decompress Decompress
func Decompress(compressedData []byte) ([]byte, error) {
decompressedData, err := zstd.Decompress(nil, compressedData)
return decompressedData, err
}
package: .
import:
- package: github.com/DataDog/zstd
- package: github.com/OneOfOne/xxhash
- package: github.com/allegro/bigcache
- package: github.com/dgrijalva/jwt-go
version: v3.0.0
- package: github.com/garyburd/redigo
version: v1.1.0
subpackages:
- redis
- package: github.com/go-playground/locales
version: v0.11.1
subpackages:
- en
- package: github.com/go-playground/universal-translator
version: v0.16.0
- package: github.com/gocraft/dbr
version: v2.1
- package: github.com/labstack/echo
version: v3.2.1
subpackages:
- middleware
- package: github.com/sirupsen/logrus
version: v1.0.1
- package: gopkg.in/go-playground/validator.v9
version: v9.4.0
subpackages:
- translations/en
- package: gopkg.in/mgo.v2
......@@ -30,6 +24,5 @@ import:
- bson
testImport:
- package: github.com/stretchr/testify
version: v1.1.4
subpackages:
- assert
package coreos
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"encoding/base64"
"github.com/OneOfOne/xxhash"
)
const (
IV = "AAAAAAAAAAAAAAAA"
)
//AESEncrypt Encrypts given text
func AESEncrypt(plainText, key []byte) []byte {
block, err := aes.NewCipher(key)
if err != nil {
panic(err)
}
padding := block.BlockSize() - len(plainText)%block.BlockSize()
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
plainText = append(plainText, padtext...)
cipherText := make([]byte, len(plainText))
cbc := cipher.NewCBCEncrypter(block, []byte(IV))
cbc.CryptBlocks(cipherText, plainText)
encodedData := make([]byte, base64.StdEncoding.EncodedLen(len(plainText)))
base64.StdEncoding.Encode(encodedData, cipherText)
return encodedData
}
//AESDecrypt Decrypts given cipher text
func AESDecrypt(encodedData, key []byte) []byte {
decodedData := make([]byte, base64.StdEncoding.DecodedLen(len(encodedData)))
n, err := base64.StdEncoding.Decode(decodedData, encodedData)
if err != nil {
panic(err)
}
cipherText := decodedData[:n]
block, err := aes.NewCipher(key)
if err != nil {
panic(err)
}
cbc := cipher.NewCBCDecrypter(block, []byte(IV))
cbc.CryptBlocks(cipherText, cipherText)
length := len(cipherText)
unpadding := int(cipherText[length-1])
return cipherText[:(length - unpadding)]
}
//GetHashChecksum Get Hash Check sum
func GetHashChecksum(byteArray []byte) uint64 {
h := xxhash.New64()
h.Write(byteArray)
return h.Sum64()
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment