github.com/wfusion/gofusion@v1.1.14/common/utils/cipher/algorithm_enum.go (about)

     1  package cipher
     2  
     3  import (
     4  	"github.com/spf13/cast"
     5  
     6  	"github.com/wfusion/gofusion/common/utils"
     7  )
     8  
     9  //go:generate stringer -type=Algorithm -trimprefix=Algorithm
    10  type Algorithm uint8
    11  
    12  const (
    13  	AlgorithmUnknown Algorithm = iota
    14  	AlgorithmDES
    15  	Algorithm3DES
    16  	AlgorithmAES
    17  	AlgorithmRC4
    18  	AlgorithmChaCha20poly1305
    19  	AlgorithmXChaCha20poly1305
    20  	AlgorithmSM4 // GM/T 0002-2012
    21  )
    22  
    23  var (
    24  	algorithmEnum = utils.NewEnumString[Algorithm, []Algorithm](
    25  		map[Algorithm]string{
    26  			AlgorithmDES:               "des",
    27  			Algorithm3DES:              "3des",
    28  			AlgorithmAES:               "aes",
    29  			AlgorithmRC4:               "rc4",
    30  			AlgorithmChaCha20poly1305:  "chacha20poly1305",
    31  			AlgorithmXChaCha20poly1305: "xchacha20poly1305",
    32  			AlgorithmSM4:               "sm4",
    33  		},
    34  	)
    35  )
    36  
    37  func (e Algorithm) Value() uint8 {
    38  	return uint8(e)
    39  }
    40  
    41  func (e Algorithm) String() string {
    42  	return algorithmEnum.String(e)
    43  }
    44  
    45  func (e Algorithm) IsValid() bool {
    46  	return algorithmEnum.IsValid(e)
    47  }
    48  
    49  func ParseAlgorithm(s any) Algorithm {
    50  	switch v := s.(type) {
    51  	case string:
    52  		if enumList := algorithmEnum.Enum(v); len(enumList) > 0 {
    53  			return enumList[0]
    54  		}
    55  	case Algorithm:
    56  		return v
    57  	default:
    58  		return Algorithm(cast.ToInt(s))
    59  	}
    60  	return AlgorithmUnknown
    61  }