github.com/TeaOSLab/EdgeNode@v1.3.8/internal/encrypt/method_utils.go (about) 1 package encrypt 2 3 import ( 4 "errors" 5 "reflect" 6 ) 7 8 var methods = map[string]reflect.Type{ 9 "raw": reflect.TypeOf(new(RawMethod)).Elem(), 10 "aes-128-cfb": reflect.TypeOf(new(AES128CFBMethod)).Elem(), 11 "aes-192-cfb": reflect.TypeOf(new(AES192CFBMethod)).Elem(), 12 "aes-256-cfb": reflect.TypeOf(new(AES256CFBMethod)).Elem(), 13 } 14 15 func NewMethodInstance(method string, key string, iv string) (MethodInterface, error) { 16 valueType, ok := methods[method] 17 if !ok { 18 return nil, errors.New("method '" + method + "' not found") 19 } 20 instance, ok := reflect.New(valueType).Interface().(MethodInterface) 21 if !ok { 22 return nil, errors.New("method '" + method + "' must implement MethodInterface") 23 } 24 err := instance.Init([]byte(key), []byte(iv)) 25 return instance, err 26 } 27 28 func RecoverMethodPanic(err interface{}) error { 29 if err != nil { 30 s, ok := err.(string) 31 if ok { 32 return errors.New(s) 33 } 34 35 e, ok := err.(error) 36 if ok { 37 return e 38 } 39 40 return errors.New("unknown error") 41 } 42 return nil 43 }