github.com/aacfactory/fns@v1.2.86-0.20240310083819-80d667fc0a17/commons/cryptos/des/des.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 des
    19  
    20  import (
    21  	"crypto/cipher"
    22  	"crypto/des"
    23  	"github.com/aacfactory/errors"
    24  	"github.com/aacfactory/fns/commons/cryptos/ciphers"
    25  )
    26  
    27  func NewEBC(key []byte, padding int) (v *DES, err error) {
    28  	block, parseErr := des.NewCipher(key)
    29  	if parseErr != nil {
    30  		err = errors.Warning("des: parse key failed")
    31  		return
    32  	}
    33  	v = &DES{
    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 *DES, err error) {
    42  	block, parseErr := des.NewCipher(key)
    43  	if parseErr != nil {
    44  		err = errors.Warning("des: parse key failed")
    45  		return
    46  	}
    47  	v = &DES{
    48  		key:     block,
    49  		mode:    ciphers.CBC,
    50  		padding: padding,
    51  		iv:      iv,
    52  	}
    53  	return
    54  }
    55  
    56  type DES struct {
    57  	key     cipher.Block
    58  	mode    int
    59  	padding int
    60  	iv      []byte
    61  }
    62  
    63  func (d *DES) Encrypt(plain []byte) (encrypted []byte, err error) {
    64  	switch d.mode {
    65  	case ciphers.CBC:
    66  		encrypted, err = ciphers.CBCEncrypt(d.key, plain, d.iv, d.padding)
    67  	case ciphers.EBC:
    68  		encrypted, err = ciphers.ECBEncrypt(d.key, plain, d.padding)
    69  		break
    70  	default:
    71  		err = errors.Warning("des: unsupported mode")
    72  		break
    73  	}
    74  	if err != nil {
    75  		err = errors.Warning("des: encrypt failed").WithCause(err)
    76  	}
    77  	return
    78  }
    79  
    80  func (d *DES) Decrypt(encrypted []byte) (plain []byte, err error) {
    81  	switch d.mode {
    82  	case ciphers.CBC:
    83  		plain, err = ciphers.CBCDecrypt(d.key, encrypted, d.iv, d.padding)
    84  	case ciphers.EBC:
    85  		plain, err = ciphers.ECBDecrypt(d.key, encrypted, d.padding)
    86  		break
    87  	default:
    88  		err = errors.Warning("des: unsupported mode")
    89  		break
    90  	}
    91  	if err != nil {
    92  		err = errors.Warning("des: decrypt failed").WithCause(err)
    93  	}
    94  	return
    95  }