项目作者: vdparikh

项目描述 :
GoLang Crypto operations - AES, HMAC, Checksum and RSA Encrypt Decrypt
高级语言: Go
项目地址: git://github.com/vdparikh/crypto.git
创建时间: 2018-02-26T18:12:28Z
项目社区:https://github.com/vdparikh/crypto

开源协议:The Unlicense

下载


crypto

A base crypto package for GoLang. Provides easy crypto operations for - AES, HMAC, Checksum and RSA Encrypt Decrypt

Go Report Card

  1. go get -u github.com/vdparikh/crypto

Tests

  1. go test -v
  2. === RUN TestAesEncryptDecrypt
  3. --- PASS: TestAesEncryptDecrypt (0.00s)
  4. === RUN TestRsaEncryptDecrypt
  5. --- PASS: TestRsaEncryptDecrypt (0.00s)
  6. === RUN TestHmac
  7. --- PASS: TestHmac (0.00s)
  8. === RUN TestChecksum
  9. --- PASS: TestChecksum (0.00s)
  10. PASS
  11. ok crypto/crypto 0.029s

Benchmarks

  1. go test -bench=.
  2. goos: darwin
  3. goarch: amd64
  4. pkg: github.com/vdparikh/crypto
  5. BenchmarkAesEncrypt-8 1000000 1948 ns/op
  6. BenchmarkAesDecrypt-8 1000000 1175 ns/op
  7. BenchmarkRsaEncrypt-8 20000 96785 ns/op
  8. BenchmarkRsaDecrypt-8 10000 210501 ns/op
  9. BenchmarkHmac-8 500000 2535 ns/op
  10. BenchmarkChecksum-8 3000000 496 ns/op
  11. PASS
  12. ok github.com/vdparikh/crypto 13.315s

Coverage

  1. go test -cover
  2. PASS
  3. coverage: 77.0% of statements
  4. ok github.com/vdparikh/crypto 0.029s

Usage

Checkout the example file in the example folder

  1. package main
  2. import (
  3. "bytes"
  4. "encoding/base64"
  5. "io"
  6. "io/ioutil"
  7. "log"
  8. "os"
  9. "github.com/vdparikh/crypto"
  10. )
  11. /*
  12. ## Creating RSA Public/Private Key Pair
  13. openssl genpkey -algorithm RSA -out private_key.pem -pkeyopt rsa_keygen_bits:2048
  14. openssl rsa -in private_key.pem -out private_key.pem
  15. openssl rsa -pubout -in private_key.pem -out public_key.pem
  16. */
  17. func main() {
  18. key := []byte("1234543444555666")
  19. cryptValue := "This is a long text which will be encrypted"
  20. err := ioutil.WriteFile("/tmp/temp_file", []byte(cryptValue), 0644)
  21. log.Println("Performing Operations on ", cryptValue)
  22. // AES Encrypt
  23. encryptedValue, err := crypto.AesEncrypt([]byte(cryptValue), key)
  24. if err == nil {
  25. log.Print("Encrypted Value: ", encryptedValue)
  26. }
  27. decryptedValue, err := crypto.AesDecrypt(encryptedValue, key)
  28. if err == nil {
  29. log.Println("Decrypted Value: ", string(decryptedValue))
  30. }
  31. hmacValue := crypto.HmacValue([]byte(cryptValue), key)
  32. if err == nil {
  33. log.Println("HMAC Value: ", hmacValue)
  34. }
  35. checksum := crypto.Checksum([]byte(cryptValue))
  36. if err == nil {
  37. log.Println("Checksum Value: ", checksum)
  38. }
  39. checksumFile := crypto.ChecksumFile("/tmp/temp_file")
  40. if err == nil {
  41. log.Println("Checksum File Value: ", checksumFile)
  42. }
  43. pubKeyBytes := readFile("public_key.pem")
  44. rsaEncryptedValue, err := crypto.RsaEncrypt([]byte(cryptValue), pubKeyBytes)
  45. if err == nil {
  46. rsaEncryptedValueEncoded := base64.StdEncoding.EncodeToString(rsaEncryptedValue)
  47. log.Println("RSA Encrypted Value: ", rsaEncryptedValueEncoded)
  48. }
  49. privKeyBytes := readFile("private_key.pem")
  50. rsaDecryptedValue, err := crypto.RsaDecrypt(rsaEncryptedValue, privKeyBytes)
  51. if err == nil {
  52. log.Println("RSA Encrypted Value: ", string(rsaDecryptedValue))
  53. }
  54. }
  55. func readFile(filename string) []byte {
  56. buf := bytes.NewBuffer(nil)
  57. f, _ := os.Open(filename)
  58. io.Copy(buf, f)
  59. f.Close()
  60. s := buf.Bytes()
  61. return s
  62. }

Sample Script Output

  1. go run main.go
  2. INFO[0000] Performing Operations on This is a long text which will be encrypted
  3. INFO[0000] Encrypted Value: g6Fj2gAwOKX9QpLyER/5yhLh8it7sw8wxOrlFe2o/AHDd+N/HequAMJXA2Pau2CwhXB68rRvoWmwZaukNK5L2zp3RWbUxd+UDaFroA==
  4. INFO[0000] Decrypted Value: This is a long text which will be encrypted
  5. INFO[0000] HMAC Value: ZTFiNTFlYTQyYWFmNzI2ZTY5MDQ5NzYzODQ4OTU3MzEwOWQyNTNlNjhjMmMwN2U1MDVhZGE0ZThhNjkwMWI0YjVjMDAzOWNjMmZlNzQ3NDdhYjI3OWJjZTM5NTk1MjFiNDU1OTA5YjI4ZGRlZDEzYjYxMmE5NjI0OTJjMmNhNjY=
  6. INFO[0000] Checksum Value: 091d3d091d2e8359cb56a881a3e2c92c5a6708996a017dcbf17979119fb630ca
  7. INFO[0000] Checksum File Value: 091d3d091d2e8359cb56a881a3e2c92c5a6708996a017dcbf17979119fb630ca
  8. INFO[0000] RSA Encrypted Value: nKVVuCY7bPzNvfCx+NCa/3QYiliinc2Jhuvf7ZQTc87ZvcDOWiQwvXfkicLLv9WqqjmvzxWGTqxeJvN9Gw9SzRUAadgeQapS4VRR5VoTYsIEs8ye9yyyWzeAf6tp1bsj6GclE3MozPYcC4GMeeyGsrVb1JReNboUxZYOYd5wdqAwwG9MJtaq7pO2rFE7vkP3TGBlP53DzjAttFTilGV/2IbvyRmGUZsuyrKc4nJt+wvzPUVulzMcnqD9wRBPkAJ66SnxK/floYeCLt7U006om+xr19R+JKjLtzO9SDy8YNsv5++jUYhKcjfcts3BSExqO+HhJ5inswr9uRsOvJrvPg==
  9. INFO[0000] RSA Encrypted Value: This is a long text which will be encrypted