crypto

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 13, 2023 License: MIT Imports: 20 Imported by: 0

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)
}

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.Md5Hash(example))
  fmt.Println("Sha1: " + crypto.Sha1Hash(example))
  fmt.Println("Sha256: " + crypto.Sha256Hash(example))
  fmt.Println("Sha512: " + crypto.Sha512Hash(example))
}

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 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 Md5Hash

func Md5Hash(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 Sha1Hash

func Sha1Hash(plaintext []byte) string

func Sha256Hash

func Sha256Hash(plaintext []byte) string

func Sha512Hash

func Sha512Hash(plaintext []byte) string

func VerifyBcrypt

func VerifyBcrypt(hash []byte, plaintext []byte) 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