github.com/aacfactory/fns@v1.2.86-0.20240310083819-80d667fc0a17/commons/cryptos/aes/aes.go (about)

     1  /*
     2   * Copyright 2023 Wang Min Xiang
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   * 	http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   *
    16   */
    17  
    18  package aes
    19  
    20  import (
    21  	"crypto/aes"
    22  	"crypto/cipher"
    23  	"github.com/aacfactory/errors"
    24  	"github.com/aacfactory/fns/commons/cryptos/ciphers"
    25  )
    26  
    27  func NewEBC(key []byte, padding int) (v *AES, err error) {
    28  	block, parseErr := aes.NewCipher(key)
    29  	if parseErr != nil {
    30  		err = errors.Warning("aes: parse key failed")
    31  		return
    32  	}
    33  	v = &AES{
    34  		key:     block,
    35  		mode:    ciphers.EBC,
    36  		padding: padding,
    37  	}
    38  	return
    39  }
    40  
    41  func NewCBC(key []byte, iv []byte, padding int) (v *AES, err error) {
    42  	block, parseErr := aes.NewCipher(key)
    43  	if parseErr != nil {
    44  		err = errors.Warning("aes: parse key failed")
    45  		return
    46  	}
    47  	v = &AES{
    48  		key:     block,
    49  		mode:    ciphers.CBC,
    50  		padding: padding,
    51  		iv:      iv,
    52  	}
    53  	return
    54  }
    55  
    56  type AES struct {
    57  	key     cipher.Block
    58  	mode    int
    59  	padding int
    60  	iv      []byte
    61  }
    62  
    63  func (a *AES) Encrypt(plain []byte) (encrypted []byte, err error) {
    64  	switch a.mode {
    65  	case ciphers.CBC:
    66  		encrypted, err = ciphers.CBCEncrypt(a.key, plain, a.iv, a.padding)
    67  	case ciphers.EBC:
    68  		encrypted, err = ciphers.ECBEncrypt(a.key, plain, a.padding)
    69  		break
    70  	default:
    71  		err = errors.Warning("aes: unsupported mode")
    72  		break
    73  	}
    74  	if err != nil {
    75  		err = errors.Warning("aes: encrypt failed").WithCause(err)
    76  	}
    77  	return
    78  }
    79  
    80  func (a *AES) Decrypt(encrypted []byte) (plain []byte, err error) {
    81  	switch a.mode {
    82  	case ciphers.CBC:
    83  		plain, err = ciphers.CBCDecrypt(a.key, encrypted, a.iv, a.padding)
    84  	case ciphers.EBC:
    85  		plain, err = ciphers.ECBDecrypt(a.key, encrypted, a.padding)
    86  		break
    87  	default:
    88  		err = errors.Warning("aes: unsupported mode")
    89  		break
    90  	}
    91  	if err != nil {
    92  		err = errors.Warning("aes: decrypt failed").WithCause(err)
    93  	}
    94  	return
    95  }