-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c48eb04
commit f6b3572
Showing
13 changed files
with
653 additions
and
91 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
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
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,73 @@ | ||
package utils | ||
|
||
import ( | ||
"crypto/aes" | ||
"crypto/cipher" | ||
"crypto/rand" | ||
"encoding/hex" | ||
"fmt" | ||
"io" | ||
) | ||
|
||
func Encrypt(stringToEncrypt string, keyString string) (encryptedString string) { | ||
|
||
//Since the key is in string, we need to convert decode it to bytes | ||
key, _ := hex.DecodeString(keyString) | ||
plaintext := []byte(stringToEncrypt) | ||
|
||
//Create a new Cipher Block from the key | ||
block, err := aes.NewCipher(key) | ||
if err != nil { | ||
panic(err.Error()) | ||
} | ||
|
||
//Create a new GCM - https://en.wikipedia.org/wiki/Galois/Counter_Mode | ||
//https://golang.org/pkg/crypto/cipher/#NewGCM | ||
aesGCM, err := cipher.NewGCM(block) | ||
if err != nil { | ||
panic(err.Error()) | ||
} | ||
|
||
//Create a nonce. Nonce should be from GCM | ||
nonce := make([]byte, aesGCM.NonceSize()) | ||
if _, err = io.ReadFull(rand.Reader, nonce); err != nil { | ||
panic(err.Error()) | ||
} | ||
|
||
//Encrypt the data using aesGCM.Seal | ||
//Since we don't want to save the nonce somewhere else in this case, we add it as a prefix to the encrypted data. The first nonce argument in Seal is the prefix. | ||
ciphertext := aesGCM.Seal(nonce, nonce, plaintext, nil) | ||
return fmt.Sprintf("%x", ciphertext) | ||
} | ||
|
||
func Decrypt(encryptedString string, keyString string) (decryptedString string) { | ||
|
||
key, _ := hex.DecodeString(keyString) | ||
enc, _ := hex.DecodeString(encryptedString) | ||
|
||
//Create a new Cipher Block from the key | ||
block, err := aes.NewCipher(key) | ||
if err != nil { | ||
panic(err.Error()) | ||
} | ||
|
||
//Create a new GCM | ||
aesGCM, err := cipher.NewGCM(block) | ||
if err != nil { | ||
panic(err.Error()) | ||
} | ||
|
||
//Get the nonce size | ||
nonceSize := aesGCM.NonceSize() | ||
|
||
//Extract the nonce from the encrypted data | ||
nonce, ciphertext := enc[:nonceSize], enc[nonceSize:] | ||
|
||
//Decrypt the data | ||
plaintext, err := aesGCM.Open(nil, nonce, ciphertext, nil) | ||
if err != nil { | ||
panic(err.Error()) | ||
} | ||
|
||
return fmt.Sprintf("%s", plaintext) | ||
} |
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,80 @@ | ||
package xopen | ||
|
||
import ( | ||
"compress/gzip" | ||
"io" | ||
"os/exec" | ||
) | ||
|
||
// from: https://gist.github.com/rasky/d42a52c16683f1a2f4dccdef80e2712d | ||
|
||
// fastGzReader is an API-compatible drop-in replacement | ||
// for gzip.Reader, that achieves a higher decoding speed | ||
// by spawning an external gzip instance and pipeing data | ||
// through it. | ||
// Go's native gzip implementation is about 2x slower at | ||
// decompressing data compared to zlib (mostly due to Go compiler | ||
// inefficiencies). So for tasks where the gzip decoding | ||
// speed is important, this is a quick workaround that doesn't | ||
// require cgo. | ||
// gzip is part of the gzip package and comes preinstalled on | ||
// most Linux distributions and on OSX. | ||
type fastGzReader struct { | ||
io.ReadCloser | ||
} | ||
|
||
func hasProg(prog ...string) bool { | ||
var cmd *exec.Cmd | ||
if len(prog) > 1 { | ||
cmd = exec.Command(prog[0], prog[1:]...) | ||
} else { | ||
cmd = exec.Command(prog[0]) | ||
} | ||
err := cmd.Start() | ||
has := err == nil | ||
cmd.Wait() | ||
return has | ||
} | ||
|
||
var hasZlib = hasProg("gzip", "-d") | ||
var hasPigz = hasProg("pigz", "-d") | ||
|
||
func newFastGzReader(r io.Reader) (io.ReadCloser, error) { | ||
|
||
if hasZlib || hasPigz { | ||
var gz fastGzReader | ||
if err := gz.Reset(r); err != nil { | ||
return nil, err | ||
} | ||
return gz, nil | ||
} | ||
return gzip.NewReader(r) | ||
|
||
} | ||
|
||
func (gz *fastGzReader) Reset(r io.Reader) error { | ||
if gz.ReadCloser != nil { | ||
gz.Close() | ||
} | ||
var cmd *exec.Cmd | ||
if hasPigz { | ||
cmd = exec.Command("pigz", "-d") | ||
} else { | ||
cmd = exec.Command("gzip", "-d") | ||
} | ||
cmd.Stdin = r | ||
|
||
rpipe, err := cmd.StdoutPipe() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = cmd.Start() | ||
if err != nil { | ||
rpipe.Close() | ||
return err | ||
} | ||
|
||
gz.ReadCloser = rpipe | ||
return nil | ||
} |
Oops, something went wrong.