cryptutil

package module
v0.0.0-...-9f2812f Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2021 License: MIT Imports: 10 Imported by: 0

README

codecov Build Status go.dev Go Report Card Licenses

CryptUtil

crypt algorithm wrapper for easy use RSA,AES,DES and others.

Basic Usage

Installation

To get the package, execute:

go get github.com/gofika/cryptutil
DES
package main

import (
  "fmt"
  "github.com/gofika/cryptutil"
)

func main() {
  // your des key
  key := cryptutil.DESKey{1, 2, 3, 4, 5, 6, 7, 8}

  des := cryptutil.NewDES(key)
  content := "Foo"
  // CFB encrypt
  encrypted := des.CFBEncrypt([]byte(content))
  fmt.Println(encrypted)
  // CFG decrypt
  decrypted := des.CFBDecrypt(encrypted)
  fmt.Println(decrypted)
}

Documentation

Index

Constants

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

Variables

This section is empty.

Functions

func PKCS7Padding

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

PKCS7Padding padding bytes with PKCS7

func PKCS7Trimming

func PKCS7Trimming(buf []byte) ([]byte, error)

PKCS7Trimming trimming bytes with PKCS7

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.

Types

type AES

type AES struct {
	// contains filtered or unexported fields
}

func NewAES128

func NewAES128(key AES128Key) *AES

NewAES128 cipher with AES-128

func NewAES192

func NewAES192(key AES192Key) *AES

NewAES192 cipher with AES-192

func NewAES256

func NewAES256(key AES256Key) *AES

NewAES256 cipher with AES-256

func (*AES) CBCDecrypt

func (a *AES) CBCDecrypt(buf []byte) ([]byte, error)

CBCDecrypt decrypt with CBC cipher. trimming with PKCS7

func (*AES) CBCEncrypt

func (a *AES) CBCEncrypt(buf []byte) []byte

CBCEncrypt encrypt with CBC cipher. padding with PKCS7

func (*AES) CFBDecrypt

func (a *AES) CFBDecrypt(buf []byte) []byte

CFBDecrypt decrypt with CFB cipher

func (*AES) CFBEncrypt

func (a *AES) CFBEncrypt(buf []byte) []byte

CFBEncrypt encrypt with CFB cipher

type AES128Key

type AES128Key [16]byte

func GenerateAES128Key

func GenerateAES128Key() (ret AES128Key)

GenerateAES128Key generate random AES128Key

func (AES128Key) String

func (k AES128Key) String() string

type AES192Key

type AES192Key [24]byte

func GenerateAES192Key

func GenerateAES192Key() (ret AES192Key)

GenerateAES192Key generate random AES192Key

func (AES192Key) String

func (k AES192Key) String() string

type AES256Key

type AES256Key [32]byte

func GenerateAES256Key

func GenerateAES256Key() (ret AES256Key)

GenerateAES256Key generate random AES256Key

func (AES256Key) String

func (k AES256Key) String() string

type DES

type DES struct {
	// contains filtered or unexported fields
}

func NewDES

func NewDES(key DESKey) *DES

func (*DES) CBCDecrypt

func (d *DES) CBCDecrypt(buf []byte) ([]byte, error)

CBCDecrypt decrypt with CBC cipher. trimming with PKCS7

func (*DES) CBCEncrypt

func (d *DES) CBCEncrypt(buf []byte) []byte

CBCEncrypt encrypt with CBC cipher. padding with PKCS7

func (*DES) CFBDecrypt

func (d *DES) CFBDecrypt(buf []byte) []byte

CFBDecrypt decrypt with CFB cipher

func (*DES) CFBEncrypt

func (d *DES) CFBEncrypt(buf []byte) []byte

CFBEncrypt encrypt with CFB cipher

type DESKey

type DESKey [8]byte

func GenerateDESKey

func GenerateDESKey() (ret DESKey)

GenerateDESKey generate random DESKey

func (DESKey) String

func (k DESKey) String() string

Jump to

Keyboard shortcuts

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