cryptoutil

package module
v0.0.0-...-dde13ab Latest Latest
Warning

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

Go to latest
Published: Aug 1, 2022 License: MIT Imports: 12 Imported by: 0

README

codecov Build Status go.dev Go Report Card Licenses

CryptoUtil

Crypto algorithm wrapper for easy use. ex: AES,DES and others.

Basic Usage

Installation

To get the package, execute:

go get -u github.com/gofika/cryptoutil
BlockReader
package main

import (
    "crypto/aes"
    "crypto/cipher"
    "fmt"
    "io"
    "math/rand"

    "github.com/gofika/cryptoutil"
)

func main() {
    // generate random iv for testing
    randIV := func() []byte {
        t := [aes.BlockSize]byte{}
        _, _ = rand.Read(t[:])
        return t[:]
    }
    iv := randIV()
    // generate random aes key for testing
    key := cryptoutil.GenerateAES256Key()

    f, _ := os.Open("src.data")
    defer f.Close()
    // target
    decrypted, _ := os.Create("decrypted.data")
    defer decrypted.Close()
    block, _ := aes.NewCipher(key)
    // block read wrapper for io.Reader
    r := cryptoutil.NewBlockReader(cipher.NewCBCDecrypter(block, iv), f)
    n, _ := io.Copy(decrypted, r)
    fmt.Printf("decrypted size: %d\n", n)
}
BlockWriter
package main

import (
    "crypto/aes"
    "crypto/cipher"
    "fmt"
    "io"
    "math/rand"

    "github.com/gofika/cryptoutil"
)

func main() {
    // generate random iv for testing
    randIV := func() []byte {
        t := [aes.BlockSize]byte{}
        _, _ = rand.Read(t[:])
        return t[:]
    }
    iv := randIV()
    // generate random aes key for testing
    key := cryptoutil.GenerateAES256Key()

    f, _ := os.Open("src.data")
    defer f.Close()
    // target
    encrypted, _ := os.Create("encrypted.data")
    defer encrypted.Close()
    block, _ := aes.NewCipher(key)
    // block wrapper for io.Writer
    w := cryptoutil.NewBlockWriter(cipher.NewCBCEncrypter(block, iv), encrypted)
    n, _ := io.Copy(w, f)
    // Note: Because it is block encryption, you need to manually call Close() when closing to write the block content of the cache
    w.Close()
    fmt.Printf("encrypted size: %d\n", n)
}
AES
package main

import (
    "crypto/aes"
    "fmt"

    "github.com/gofika/cryptoutil"
)

func main() {
    randIV := func() []byte {
        t := [aes.BlockSize]byte{}
        _, _ = rand.Read(t[:])
        return t[:]
    }
    iv := randIV()
    // generate random aes key for testing
    key := cryptoutil.GenerateAES256Key()

    // encrypt/decrypt bytes
    data := []byte("foo")
    encoded, _ := cryptoutil.AESEncrypt(data, key, iv)
    fmt.Printf("bytes encoded: %v\n", encoded)
    decoded, _ := cryptoutil.AESDecrypt(encoded, key, iv)
    fmt.Printf("bytes decoded: %s\n", decoded)

    // encrypt/decrypt string
    dataStr := "foo"
    encodedStr, _ := cryptoutil.AESEncryptString(dataStr, key, iv)
    fmt.Printf("string encoded: %v\n", encodedStr)
    decodedStr, err := cryptoutil.AESDecryptString(encodedStr, key, iv)
    fmt.Printf("string decoded: %s\n", decodedStr)
}
DES
package main

import (
    "crypto/des"
    "fmt"

    "github.com/gofika/cryptoutil"
)

func main() {
    randIV := func() []byte {
        t := [des.BlockSize]byte{}
        _, _ = rand.Read(t[:])
        return t[:]
    }
    iv := randIV()
    // generate random des key for testing
    key := cryptoutil.GenerateDESKey()

    // encrypt/decrypt bytes
    data := []byte("foo")
    encoded, _ := cryptoutil.DESEncrypt(data, key, iv)
    fmt.Printf("bytes encoded: %v\n", encoded)
    decoded, _ := cryptoutil.DESDecrypt(encoded, key, iv)
    fmt.Printf("bytes decoded: %s\n", decoded)

    // encrypt/decrypt string
    dataStr := "foo"
    encodedStr, _ := cryptoutil.DESEncryptString(dataStr, key, iv)
    fmt.Printf("string encoded: %v\n", encodedStr)
    decodedStr, err := cryptoutil.DESDecryptString(encodedStr, key, iv)
    fmt.Printf("string decoded: %s\n", decodedStr)
}

Documentation

Index

Constants

View Source
const (
	// RSA default E
	RSADefaultExponent = 65537
)

Variables

This section is empty.

Functions

func AESDecrypt

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

AESDecrypt

func AESDecryptString

func AESDecryptString(s string, key []byte, iv []byte) (string, error)

AESDecryptString

func AESEncrypt

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

AESEncrypt

func AESEncryptString

func AESEncryptString(s string, key []byte, iv []byte) (string, error)

AESEncryptString

func DESDecrypt

func DESDecrypt(s []byte, key []byte, iv []byte) ([]byte, error)

DESDecrypt

func DESDecryptString

func DESDecryptString(s string, key []byte, iv []byte) (string, error)

DESDecryptString

func DESEncrypt

func DESEncrypt(s []byte, key []byte, iv []byte) ([]byte, error)

DESEncrypt

func DESEncryptString

func DESEncryptString(s string, key []byte, iv []byte) (string, error)

DESEncryptString

func GenerateAES128Key

func GenerateAES128Key() []byte

GenerateAES128Key generate random AES128Key

func GenerateAES192Key

func GenerateAES192Key() []byte

GenerateAES192Key generate random AES192Key

func GenerateAES256Key

func GenerateAES256Key() []byte

GenerateAES256Key generate random AES256Key

func GenerateDESKey

func GenerateDESKey() []byte

GenerateDESKey generate random DESKey

func GenerateTripleDESKey

func GenerateTripleDESKey() []byte

GenerateTripleDESKey generate random TripleDESKey

func PKCS5Padding

func PKCS5Padding(ciphertext []byte) []byte

func PKCS7Padding

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

func PKCSTrimming

func PKCSTrimming(ciphertext []byte) []byte

func RSAEncrypt

func RSAEncrypt(buf []byte, key []byte) ([]byte, error)

RSAEncrypt encrypt buf with PKCS1v15

func RSAEncryptNoPadding

func RSAEncryptNoPadding(buf []byte, key *rsa.PublicKey) []byte

RSAEncryptNoPadding calc rsa encrypt with no padding

func RSAEncryptPKCS1v15

func RSAEncryptPKCS1v15(buf []byte, key *rsa.PublicKey) ([]byte, error)

RSAEncryptPKCS1v15 encrypt buf with PKCS1v15

func RSAPublicKeyFromBytes

func RSAPublicKeyFromBytes(n []byte, e int) *rsa.PublicKey

RSAPublicKeyFromBytes create *rsa.PublicKey from n bytes. if e zero use 65537

func RSAPublicKeyFromString

func RSAPublicKeyFromString(n string, e int, base int) (*rsa.PublicKey, error)

RSAPublicKeyFromString create *rsa.PublicKey from n string. if e zero use 65537 The base argument must be 0 or a value between 2 and MaxBase. For base 0, the number prefix determines the actual base: A prefix of “0b” or “0B” selects base 2, “0”, “0o” or “0O” selects base 8, and “0x” or “0X” selects base 16. Otherwise, the selected base is 10 and no prefix is accepted. For bases <= 36, lower and upper case letters are considered the same: The letters 'a' to 'z' and 'A' to 'Z' represent digit values 10 to 35. For bases > 36, the upper case letters 'A' to 'Z' represent the digit values 36 to 61. For base 0, an underscore character “_” may appear between a base prefix and an adjacent digit, and between successive digits; such underscores do not change the value of the number. Incorrect placement of underscores is reported as an error if there are no other errors. If base != 0, underscores are not recognized and act like any other character that is not a valid digit.

func TripleDESDecrypt

func TripleDESDecrypt(s []byte, key []byte, iv []byte) ([]byte, error)

TripleDESDecrypt

func TripleDESDecryptString

func TripleDESDecryptString(s string, key []byte, iv []byte) (string, error)

TripleDESDecryptString

func TripleDESEncrypt

func TripleDESEncrypt(s []byte, key []byte, iv []byte) ([]byte, error)

TripleDESEncrypt

func TripleDESEncryptString

func TripleDESEncryptString(s string, key []byte, iv []byte) (string, error)

TripleDESEncryptString

Types

type AES128Key

type AES128Key [aes.BlockSize]byte

type AES192Key

type AES192Key [24]byte

type AES256Key

type AES256Key [32]byte

type BlockReader

type BlockReader struct {
	io.ReadCloser
	// contains filtered or unexported fields
}

BlockReader block ciphertext streaming reader

func NewAESReader

func NewAESReader(r io.Reader, key []byte, iv []byte) (*BlockReader, error)

NewAESReader creates and returns a new BlockReader. The key argument should be the AES key, either 16, 24, or 32 bytes to select AES-128, AES-192, or AES-256.

func NewBlockReader

func NewBlockReader(blockMode cipher.BlockMode, src io.Reader) *BlockReader

wrap BlockReader block ciphertext streaming reader

func NewDESReader

func NewDESReader(r io.Reader, key []byte, iv []byte) (*BlockReader, error)

NewDESReader creates and returns a new BlockReader

func NewTripleDESReader

func NewTripleDESReader(r io.Reader, key []byte, iv []byte) (*BlockReader, error)

NewTripleDESReader creates and returns a new BlockReader

func (*BlockReader) Close

func (c *BlockReader) Close() error

func (*BlockReader) Read

func (c *BlockReader) Read(data []byte) (int, error)

type BlockWriter

type BlockWriter struct {
	io.WriteCloser
	// contains filtered or unexported fields
}

BlockWriter block ciphertext streaming writer

func NewAESWriter

func NewAESWriter(dst io.Writer, k []byte, iv []byte) (*BlockWriter, error)

NewAESWriter creates and returns a new BlockWriter. The key argument should be the AES key, either 16, 24, or 32 bytes to select AES-128, AES-192, or AES-256.

func NewBlockWriter

func NewBlockWriter(blockMode cipher.BlockMode, dst io.Writer) *BlockWriter

wrap BlockReader block ciphertext streaming writer

func NewDESWriter

func NewDESWriter(dst io.Writer, k []byte, iv []byte) (*BlockWriter, error)

NewDESWriter creates and returns a new BlockWriter

func NewTripleDESWriter

func NewTripleDESWriter(dst io.Writer, k []byte, iv []byte) (*BlockWriter, error)

NewTripleDESWriter creates and returns a new BlockWriter

func (*BlockWriter) Close

func (c *BlockWriter) Close() error

Note: Because it is block encryption, you need to manually call Close() when closing to write the block content of the cache

func (*BlockWriter) Write

func (c *BlockWriter) Write(data []byte) (int, error)

type DESKey

type DESKey [des.BlockSize]byte

type KeySizeError

type KeySizeError int

func (KeySizeError) Error

func (k KeySizeError) Error() string

type ShortCiphertextSizeError

type ShortCiphertextSizeError int

func (ShortCiphertextSizeError) Error

func (s ShortCiphertextSizeError) Error() string

type TripleDESKey

type TripleDESKey [des.BlockSize * 3]byte

Jump to

Keyboard shortcuts

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