enigma

package
v0.0.0-...-e8da0a6 Latest Latest
Warning

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

Go to latest
Published: Jul 19, 2023 License: Apache-2.0 Imports: 15 Imported by: 0

README

enigma

config file

blockMethod: AES
blockSize: 128
blockKey: YnJvd24gZm94IGp1bXBzIG92ZXIgdGhlIGxhenkgZG9n # base64("brown fox jumps over the lazy dog")
cipherMode: GCM
cipherSalt: c2FsdHk= # base64("salty")
padding: NONE
strconv: base64

usage

func main() {
    NewString := func(s string) *string { return &s }
    
    config := enigma.Config{}
    config.EncryptionMethod = "AES"
    config.BlockSize = 128
    config.BlockKey = "YnJvd24gZm94IGp1bXBzIG92ZXIgdGhlIGxhenkgZG9n"
    config.CipherMode = "GCM"
    config.Padding = "PKCS"
    config.CipherSalt = NewString("64uk656M7KWQIO2XjCDss4frsJTtgLTsl5Ag7YOA6rOg7YyM")
    config.StrConv = "base64"

    const example = "세종어제 훈민정음\n" +
        "나랏말이\n" +
        "중국과 달라\n" +
        "문자와 서로 통하지 아니하므로\n" +
        "이런 까닭으로 어리석은 백성이 이르고자 하는 바가 있어도\n" +
        "마침내 제 뜻을 능히 펴지 못하는 사람이 많다.\n" +
        "내가 이를 위해 불쌍히 여겨\n" +
        "새로 스물여덟 글자를 만드니\n" +
        "사람마다 하여금 쉬이 익혀 날마다 씀에 편안케 하고자 할 따름이다.\n"

    machine, err := enigma.NewMachine(config.ToOption())
    if err != nil {
        panic(err)
    }

    encoded, err := machine.Encode([]byte(example))
    if err != nil {
        panic(err)
    }

    plain, err := machine.Decode(encoded)
    if err != nil {
        panic(err)
    }

    if !strings.EqualFold(example, string(plain)) {
        panic("diff")
    }
}

table of configurations

  • block

    blockMethod blockSize blockKey
    NONE default(1) base64(string)
    *AES 128, 192, 256 base64(string)
    DES 64 base64(string)
  • cipher

    cipherMode blockMethod
    NONE NONE, AES, DES
    *GCM AES
    CBC NONE, AES, DES
    cipherSalt
    base64(string)
    nil

    cipherSalt를 지정되지 않은 경우 암호화 하면서 생성한 salt값을 암호화 결과의 앞에 붙여서 리턴 한다. 복호화에서는 앞에 붙어있는 salt값을 분리하여 사용

  • padding

    blockMethod cipherMode padding
    AES NONE PKCS
    AES CBC PKCS
    AES GCM NONE, PKCS
    DES NONE PKCS
    DES CBC PKCS
  • strconv

    strconv
    plain
    *base64
    hex

설정 순서

  1. 암호화 블럭을 만든다 (blockMethod, blockSize, blockKey)

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

    • 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 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, cfgset map[string]Config, insecure bool)

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

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, Decoder, error)

func (CipherMode) String

func (x CipherMode) String() string

String implements the Stringer interface.

type Config

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

blockMethod: aes # NONE, AES, DES blockSize: 128 # NONE: default(1), AES: 128|192|256, DES: 64 blockKey: secret # (base64 string) cipherMode: gcm # NONE: NONE|AES|DES , GCM: AES, CBC: NONE|AES|DES cipherSalt: null # NULL, (base64 string) padding: PKCS # NONE: AES+GCM, PKCS: AES+NONE|AES+CBC|DES+NONE|DES+CBC strconv: base64 # plain|base64|hex

func (Config) ToOption

func (cfg Config) ToOption() MachineOption

type ConfigBlock

type ConfigBlock struct {
	EncryptionMethod string `yaml:"blockMethod"` // NONE|AES|DES
	BlockSize        int    `yaml:"blockSize"`   // NONE: default(1), AES: [128|192|256], DES: [64]
	BlockKey         string `yaml:"blockKey"`    // (base64 string)
}

type ConfigCipher

type ConfigCipher struct {
	CipherMode string  `yaml:"cipherMode"` // NONE|CBC|GCM
	CipherSalt *string `yaml:"cipherSalt"` // nil: auto-generate (base64 string)
}

type ConfigPadding

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

type ConfigStrConv

type ConfigStrConv struct {
	StrConv string `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() func(key []byte) (cipher.Block, 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