enigma

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2023 License: MIT Imports: 17 Imported by: 0

README

enigma

config file

  • enigma.yml

    enigma:
    some-config:
        block-method: AES
        block-size: 128
        block-key: YnJvd24gZm94IGp1bXBzIG92ZXIgdGhlIGxhenkgZG9n # base64("brown fox jumps over the lazy dog")
        cipher-mode: GCM
        cipher-salt: c2FsdHk= # base64("salty")
        padding: NONE
        strconv: base64
    

usage

func main() {
    filename := "enigma.yml"

    config := enigma.Config{}
    if err := configor.Load(&config, filename); err != nil {
        panic(err)
    }
    if err := enigma.LoadConfig(config); err != nil {
        panic(err)
    }
}

func() {
    const some_enigma_config_name = "some-config"
    const example = "brown fox jumps over the lazy dog"
    
    encoded, _ := enigma.GetMachine(some_enigma_config_name).Encode([]byte(example))
    fmt.Println("encoded:", string(encoded)) 
    
    plain, _ := enigma.GetMachine(some_enigma_config_name).Decode(encoded)
    fmt.Println("plain:", string(plain)) 

    if strings.EqualFold(example, plain) {
        panic("diff")
    }
}
output
encoded: nuZFj5ox3VvUUh12bgAnLdmo+Jg1cKZSonlY4nm7KBajBAz6w+yyR6VXDJdXfwxxwA==
plain: brown fox jumps over the lazy dog

table of config

  • block

    block-method block-size block-key
    NONE default(1) base64(string)
    *AES 128, 192, 256 base64(string)
    DES 64 base64(string)
  • cipher

    cipher-mode block-method
    NONE NONE, AES, DES
    *GCM AES
    CBC NONE, AES, DES
    • cipher-salt: [base64(string) | (null)]

      지정되지 않은 경우 암호화 하면서 생성한 salt값을 암호화 결과의 앞에 붙여서 리턴 한다

      복호화 하면서 앞에 붙어있는 salt값을 분리하여 복호화에서 사용

  • padding

    padding block-method+cipher-mode
    *NONE AES+GCM
    PKCS AES+NONE, AES+GCM, AES+CBC,
    DES+NONE, DES+CBC
  • strconv

    strconv
    plain
    *base64
    hex

설정 순서

  1. 암호화 블럭을 만든다 (block-method, block-size, block-key)

  2. 암호화 cipher를 만든다 (cipher-mode)

    • GCM: 나중에 nonce를 생성하기 위해서 cipher.AEAD.NonceSize() 값을 저장

    • CBC: 나중에 iv를 생성하기 위해서 cipher.Block.BlockSize() 값을 저장

  3. 암호화 블럭과 cipher를 이용하여 enigma.Machine에서 이용하는 Encoder, Decoder 함수를 생성하여 enigma.Machine 생성

암호화 순서

func (machine *Machine) Encode(src []byte) ([]byte, error)
  1. 암화와 블럭 사이즈 만큼 입력값에 패드 추가

  2. Encoder 실행

  3. salt encode rule 적용; salt 값이 null이면 암호화 결과에 salt를 앞에 붙이는 작업

  4. strconv encode; 지정된 변환 설정에 따라 []byte 결과를 인코드 한다

복호화 순서 (조립의 역순)

func (machine *Machine) Decode(src []byte) ([]byte, error)
  1. strconv decode; 지정된 변환 설정에 따라 []byte 결과를 디코드 한다

  2. salt decode rule 적용; salt 값이 null이면 암호화 결과에서 앞에 저장된 salt를 분리하는 작업

  3. Decoder 실행

  4. 암화와 블럭 사이즈 만큼 입력값에 패드 제거

Documentation

Index

Constants

View Source
const (
	BlockSize_AES128 BlockSize_AES = 128 / 8
	BlockSize_AES192               = 192 / 8
	BlockSize_AES256               = 256 / 8
)

Variables

This section is empty.

Functions

func CipherModeNames

func CipherModeNames() []string

CipherModeNames returns a list of possible string values of CipherMode.

func EncryptionMethodNames

func EncryptionMethodNames() []string

EncryptionMethodNames returns a list of possible string values of EncryptionMethod.

func LoadConfig

func LoadConfig(cfg Config) error

func PKCS7Padding

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

func PKCS7Unpadding

func PKCS7Unpadding(src []byte) []byte

func PaddingNames

func PaddingNames() []string

PaddingNames returns a list of possible string values of Padding.

func PrintConfig

func PrintConfig(w io.Writer, cfg Config)

func RandBytes

func RandBytes(n int) (b []byte, err error)

func SaltDecodeRule

func SaltDecodeRule(src []byte, salt []byte, has bool) (src_, salt_ []byte)

func SaltEncodeRule

func SaltEncodeRule(src []byte, salt []byte, has bool) (src_ []byte)

func StrConvNames

func StrConvNames() []string

StrConvNames returns a list of possible string values of StrConv.

Types

type BlockSize_AES

type BlockSize_AES int

type BlockSize_DES

type BlockSize_DES int
const (
	BlockSize_DES64 BlockSize_DES = 64 / 8
)

type Cipher

type Cipher interface {
	EncodeDetail(src []byte, callback ...func(map[string]interface{})) ([]byte, error)
	Encode(src []byte) ([]byte, error)
	DecodeDetail(src []byte, callback ...func(map[string]interface{})) ([]byte, error)
	Decode(src []byte) ([]byte, error)
}

func CipherSet

func CipherSet(k string) Cipher

type CipherMode

type CipherMode int
ENUM(

NONE CBC GCM )

const (
	// CipherModeNONE is a CipherMode of type NONE.
	CipherModeNONE CipherMode = iota
	// CipherModeCBC is a CipherMode of type CBC.
	CipherModeCBC
	// CipherModeGCM is a CipherMode of type GCM.
	CipherModeGCM
)

func ParseCipherMode

func ParseCipherMode(name string) (CipherMode, error)

ParseCipherMode attempts to convert a string to a CipherMode.

func (CipherMode) CipherFactory

func (mode CipherMode) CipherFactory(block cipher.Block, salt *Salt) (encoder Encoder, decoder Decoder, err error)

func (CipherMode) String

func (x CipherMode) String() string

String implements the Stringer interface.

type Config

type Config struct {
	CryptoAlgorithmSet map[string]ConfigCryptoAlgorithm `yaml:"enigma"`
}

Config

config-name:
  block-method: aes    # NONE, AES, DES
  block-size: 128      # NONE: default(1), AES: 128|192|256, DES: 64
  block-key: secret    # (base64 string)
  cipher-mode: gcm     # NONE: NONE|AES|DES , GCM: AES, CBC: NONE|AES|DES
  cipher-salt: null    # NULL, (base64 string)
  padding: PKCS        # NONE: AES+GCM, PKCS: AES+NONE|AES+CBC|DES+NONE|DES+CBC
  strconv: base64      # plain|base64|hex

type ConfigBlock

type ConfigBlock struct {
	EncryptionMethod string `env:"ENIGMA_BLOCK_METHOD" yaml:"block-method"` // NONE|AES|DES
	BlockSize        int    `env:"ENIGMA_BLOCK_SIZE"   yaml:"block-size"`   // NONE: default(1), AES: [128|192|256], DES: [64]
	BlockKey         string `env:"ENIGMA_BLOCK_KEY"    yaml:"block-key"`    // (base64 string)
}

type ConfigCipher

type ConfigCipher struct {
	CipherMode string  `env:"ENIGMA_CIPHER_MODE" yaml:"cipher-mode"` // NONE|CBC|GCM
	CipherSalt *string `env:"ENIGMA_CIPHER_SALT" yaml:"cipher-salt"` // nil: auto-generate (base64 string)
}

type ConfigCryptoAlgorithm

type ConfigCryptoAlgorithm struct {
	ConfigBlock   `yaml:",inline"`
	ConfigCipher  `yaml:",inline"`
	ConfigPadding `yaml:",inline"`
	ConfigStrConv `yaml:",inline"`
}

func (ConfigCryptoAlgorithm) ToOption

func (cfg ConfigCryptoAlgorithm) ToOption() MachineOption

type ConfigPadding

type ConfigPadding struct {
	Padding string `env:"ENIGMA_PADDING" yaml:"padding"` // none|PKCS
}

type ConfigStrConv

type ConfigStrConv struct {
	StrConv string `env:"ENIGMA_STRCONV" yaml:"strconv"` // plain|base64|hex
}

type Decoder

type Decoder func(src, salt []byte) (dst []byte, err error)

type Encoder

type Encoder func(src, salt []byte) (dst []byte, err error)

type EncryptionMethod

type EncryptionMethod int
ENUM(

NONE AES DES )

const (
	// EncryptionMethodNONE is a EncryptionMethod of type NONE.
	EncryptionMethodNONE EncryptionMethod = iota
	// EncryptionMethodAES is a EncryptionMethod of type AES.
	EncryptionMethodAES
	// EncryptionMethodDES is a EncryptionMethod of type DES.
	EncryptionMethodDES
)

func ParseEncryptionMethod

func ParseEncryptionMethod(name string) (EncryptionMethod, error)

ParseEncryptionMethod attempts to convert a string to a EncryptionMethod.

func (EncryptionMethod) BlockFactory

func (method EncryptionMethod) BlockFactory() (fn func(key []byte) (cipher.Block, error), err error)

func (EncryptionMethod) String

func (x EncryptionMethod) String() string

String implements the Stringer interface.

type Machine

type Machine struct {
	Encoder
	Decoder
	// contains filtered or unexported fields
}

func NewMachine

func NewMachine(opt MachineOption) (m *Machine, err error)

func (*Machine) Decode

func (machine *Machine) Decode(src []byte) ([]byte, error)

func (*Machine) DecodeDetail

func (machine *Machine) DecodeDetail(src []byte, callback ...func(map[string]interface{})) (dst []byte, err error)

func (*Machine) Encode

func (machine *Machine) Encode(src []byte) ([]byte, error)

func (*Machine) EncodeDetail

func (machine *Machine) EncodeDetail(src []byte, callback ...func(map[string]interface{})) (dst []byte, err error)

type MachineOption

type MachineOption struct {
	Block struct {
		Method string `json:"block-method"`
		Size   int    `json:"block-size"`
		Key    string `json:"block-key"`
	} `json:",inline"`
	Cipher struct {
		Mode string  `json:"cipher-mode"`
		Salt *string `json:"cipher-salt,omitempty"`
	} `json:",inline"`
	Padding string `json:"padding"`
	StrConv string `json:"strconv"`
}

type NoneEncripter

type NoneEncripter struct{}

func (NoneEncripter) BlockSize

func (encripter NoneEncripter) BlockSize() int

func (NoneEncripter) Decrypt

func (encripter NoneEncripter) Decrypt(dst, src []byte)

func (NoneEncripter) Encrypt

func (encripter NoneEncripter) Encrypt(dst, src []byte)

type Padding

type Padding int
ENUM(

NONE PKCS )

const (
	// PaddingNONE is a Padding of type NONE.
	PaddingNONE Padding = iota
	// PaddingPKCS is a Padding of type PKCS.
	PaddingPKCS
)

func ParsePadding

func ParsePadding(name string) (Padding, error)

ParsePadding attempts to convert a string to a Padding.

func (Padding) Padder

func (padding Padding) Padder() func([]byte, int) []byte

func (Padding) String

func (x Padding) String() string

String implements the Stringer interface.

func (Padding) Unpadder

func (padding Padding) Unpadder() func(src []byte) (dst []byte)

type Salt

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

func (Salt) GenSalt

func (salt Salt) GenSalt() []byte

func (Salt) Has

func (salt Salt) Has() bool

func (Salt) Len

func (salt Salt) Len() int

func (Salt) Scope

func (salt Salt) Scope(fn func(*ScopeSalt) error) error

func (*Salt) SetLen

func (salt *Salt) SetLen(n int) *Salt

func (*Salt) SetValue

func (salt *Salt) SetValue(b []byte) *Salt

type ScopeSalt

type ScopeSalt struct {
	Salt
	// contains filtered or unexported fields
}

func (*ScopeSalt) GenSalt

func (salt *ScopeSalt) GenSalt() []byte

type StrConv

type StrConv int
ENUM(

plain base64 hex )

const (
	// StrConvPlain is a StrConv of type Plain.
	StrConvPlain StrConv = iota
	// StrConvBase64 is a StrConv of type Base64.
	StrConvBase64
	// StrConvHex is a StrConv of type Hex.
	StrConvHex
)

func ParseStrConv

func ParseStrConv(name string) (StrConv, error)

ParseStrConv attempts to convert a string to a StrConv.

func (StrConv) Decoder

func (conv StrConv) Decoder() func([]byte) ([]byte, error)

func (StrConv) Encoder

func (conv StrConv) Encoder() func([]byte) []byte

func (StrConv) String

func (x StrConv) String() string

String implements the Stringer interface.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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