crypto

package
v0.1.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 29, 2023 License: MIT Imports: 21 Imported by: 1

README

Cryptography Examples

This category provides different options to encode/decode and encrypt/decrypt your data with a lot of algorithms

AES

  • Encryption and decryption example
package main

import (
  "log"
  "fmt"

  "github.com/D3Ext/maldev/crypto"
)

func main() {
  plaintext := []byte("this is an example") // data to encrypt
  iv := []byte("1010101010101010") // initialization vector
  key := []byte("MySuperSecret32BitsLongPassword!") // pre-shared key

  ciphertext, err := crypto.AESEncrypt(plaintext, iv, key) // Encrypt data
  if err != nil {
    log.Fatal(err)
  }
  fmt.Println(ciphertext)

  decrypted, err := crypto.AESDecrypt(ciphertext, iv, key) // Decrypt data
  if err != nil {
    log.Fatal(err)
  }
  fmt.Println(decrypted)
}

RC4

  • Encryption and decryption example
package main

import (
  "log"
  "fmt"

  "github.com/D3Ext/maldev/crypto"
)

func main() {
  plaintext := []byte("this is an example") // data to encrypt
  key := []byte("MySecretPassword") // pre-shared key

  ciphertext, err := crypto.Rc4Encrypt(plaintext, key) // Encrypt data
  if err != nil {
    log.Fatal(err)
  }
  fmt.Println(ciphertext)

  decrypted, err := crypto.Rc4Decrypt(ciphertext, key) // Decrypt data
  if err != nil {
    log.Fatal(err)
  }
  fmt.Println(decrypted)
}

Bcrypt

  • Hash data and verify hashes
package main

import (
  "fmt"
  "log"

  "github.com/D3Ext/maldev/crypto"
)

func main() {
  plaintext := []byte("example") // Define bytes to hash

  hash, err := crypto.Bcrypt(plaintext)
  if err != nil {
    log.Fatal(err)
  }
  fmt.Println(hash)

  check := crypto.VerifyBcrypt(hash, plaintext) // Compare hash with plaintext and return a bool
  fmt.Println(check) // true
}

XOR

package main

import (
  "fmt"

  "github.com/D3Ext/maldev/crypto"
)

func main() {
  plaintext := []byte("I am the best!")
  
  xored_text := crypto.Xor(plaintext, 'x') // func Xor(buf []byte, xorchar byte) ([]byte)
  fmt.Println(xored_text)
}

ChaCha20

package main

import (
  "fmt"
  "log"
  "github.com/D3Ext/maldev/crypto"
)

func main(){
  data := []byte("this is an example")
  psk := []byte("ThisIsMySuperSecret32Password123")

  fmt.Println("Data:", string(data))
  fmt.Println("Key:", psk)

  ciphertext, err := crypto.Chacha20Encrypt(data, psk) // Encrypt
  if err != nil {
    log.Fatal(err)
  }

  fmt.Println("Ciphertext:", ciphertext)

  decoded, err := crypto.Chacha20Decrypt(ciphertext, psk) // Decrypt
  if err != nil {
    log.Fatal(err)
  }

  fmt.Println("Decoded:", string(decoded))
}

Elliptic Curve

  • Encryption and decryption example
package main

import (
  "fmt"
  "log"

  "github.com/D3Ext/maldev/crypto"
)

func main(){
  plaintext := []byte("I love malware!")
  key := []byte("MySecretKey")

  enc, err := crypto.EllipticCurveEncrypt(key, plaintext) // Encrypt bytes
  if err != nil {
    log.Fatal(err)
  }
  fmt.Println("Encrypted data:", enc)

  dec, err := crypto.EllipticCurveDecrypt(key, enc) // Decrypt ciphertext
  if err != nil {
    log.Fatal(err)
  }
  fmt.Println("Decrypted data:", dec)
}

Rot13

package main

import (
  "fmt"

  "github.com/D3Ext/maldev/crypto"
)

func main() {
  plaintext := "Example string, bla, bla, bla, bla"
  
  mod_text := crypto.Rot13(plaintext)
  fmt.Println(mod_text) // Output: Rknzcyr fgevat, oyn, oyn, oyn, oyn
}

Rot47

package main

import (
  "fmt"

  "github.com/D3Ext/maldev/crypto"
)

func main() {
  plaintext := "Example string, bla, bla, bla, bla"
  
  mod_text := crypto.Rot47(plaintext)
  fmt.Println(mod_text) // Output: "tI2>A=6 DEC:?8[ 3=2[ 3=2[ 3=2[ 3=2"
}

Base64

package main

import (
  "fmt"

  "github.com/D3Ext/maldev/crypto"
)

func main() {
  example := "This is an example"

  enc := crypto.B64E(example)
  fmt.Println(enc) // Output: "VGhpcyBpcyBhbiBleGFtcGxl"

  dec := crypto.B64D(enc)
  fmt.Println(dec) // Output: "This is an example"
}

Base32

package main

import (
  "fmt"

  "github.com/D3Ext/maldev/crypto"
)

func main() {
  example := "This is an example"

  enc := crypto.B32E(example)
  fmt.Println(enc) // Output: "KRUGS4ZANFZSAYLOEBSXQYLNOBWGK==="

  dec := crypto.B32D(enc)
  fmt.Println(dec) // Output: "This is an example"
}

Hashing

  • Examples for all supported hashes
package main

import (
  "fmt"

  "github.com/D3Ext/maldev/crypto"
)

func main() {
  example := []byte("maldev")

  fmt.Println("Md5: " + crypto.Md5(example))
  fmt.Println("Sha1: " + crypto.Sha1(example))
  fmt.Println("Sha256: " + crypto.Sha256(example))
  fmt.Println("Sha512: " + crypto.Sha512(example))
}

Verifying hashes

package main

import (
  "fmt"

  "github.com/D3Ext/maldev/crypto"
)

func main(){
  md5hash := crypto.Md5([]byte("maldev"))
  md5_check := crypto.VerifyMd5(md5hash, "maldev")
  fmt.Println(md5_check)

  sha1hash := crypto.Sha1([]byte("maldev"))
  sha1_check := crypto.VerifySha1(sha1hash, "maldev")
  fmt.Println(sha1_check)

  // Same syntax with other hashes
  // .......
}

Generate an IV

package main

import (
  "fmt"

  "github.com/D3Ext/maldev/crypto"
)

func main() {
  rand_iv, _ := crypto.GenerateIV()
  fmt.Println(rand_iv)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AESDecrypt

func AESDecrypt(ciphertext []byte, iv []byte, key []byte) ([]byte, error)

func AESEncrypt

func AESEncrypt(plaintext []byte, iv []byte, key []byte) ([]byte, error)

func B32D

func B32D(ciphertext string) string

func B32E

func B32E(plaintext string) string

func B64D

func B64D(ciphertext string) string

func B64E

func B64E(plaintext string) string

func Bcrypt

func Bcrypt(plaintext []byte) ([]byte, error)

func Chacha20Decrypt added in v0.1.4

func Chacha20Decrypt(data []byte, key []byte) ([]byte, error)

Decrypt data using given key

func Chacha20Encrypt added in v0.1.4

func Chacha20Encrypt(data []byte, key []byte) ([]byte, error)

Encrypt data using given key

func DecryptFile

func DecryptFile(file string, psk string, iv []byte) error

func EllipticCurveDecrypt

func EllipticCurveDecrypt(priv_key []byte, ciphertext []byte) ([]byte, error)

func EllipticCurveEncrypt

func EllipticCurveEncrypt(priv_key []byte, plaintext []byte) ([]byte, error)

func EncryptFile

func EncryptFile(file string, psk string, iv []byte) error

func GenerateIV

func GenerateIV() ([]byte, error)

func Md5 added in v0.1.2

func Md5(plaintext []byte) string

func Morse

func Morse(input string) (string, error)

func PKCS5Padding

func PKCS5Padding(ciphertext []byte, blockSize int) []byte

func PKCS5Trimming

func PKCS5Trimming(encrypt []byte) []byte

================== Auxiliar functions

func Rc4Decrypt

func Rc4Decrypt(ciphertext []byte, psk []byte) ([]byte, error)

func Rc4Encrypt

func Rc4Encrypt(plaintext []byte, psk []byte) ([]byte, error)

func Rot13

func Rot13(input string) string

func Rot47

func Rot47(input string) string

func Sha1 added in v0.1.2

func Sha1(plaintext []byte) string

func Sha256 added in v0.1.2

func Sha256(plaintext []byte) string

func Sha512 added in v0.1.2

func Sha512(plaintext []byte) string

func VerifyBcrypt

func VerifyBcrypt(hash []byte, plaintext []byte) bool

func VerifyMd5 added in v0.1.2

func VerifyMd5(hash string, password string) bool

func VerifySha1 added in v0.1.2

func VerifySha1(hash string, password string) bool

func VerifySha256 added in v0.1.2

func VerifySha256(hash string, password string) bool

func VerifySha512 added in v0.1.2

func VerifySha512(hash string, password string) bool

func Xor

func Xor(buf []byte, xorchar byte) []byte

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL