key_encryption_gen.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // this file was auto-generated by internal/cmd/gentypes/main.go: DO NOT EDIT
  2. package jwa
  3. import (
  4. "fmt"
  5. "sort"
  6. "sync"
  7. "github.com/pkg/errors"
  8. )
  9. // KeyEncryptionAlgorithm represents the various encryption algorithms as described in https://tools.ietf.org/html/rfc7518#section-4.1
  10. type KeyEncryptionAlgorithm string
  11. // Supported values for KeyEncryptionAlgorithm
  12. const (
  13. A128GCMKW KeyEncryptionAlgorithm = "A128GCMKW" // AES-GCM key wrap (128)
  14. A128KW KeyEncryptionAlgorithm = "A128KW" // AES key wrap (128)
  15. A192GCMKW KeyEncryptionAlgorithm = "A192GCMKW" // AES-GCM key wrap (192)
  16. A192KW KeyEncryptionAlgorithm = "A192KW" // AES key wrap (192)
  17. A256GCMKW KeyEncryptionAlgorithm = "A256GCMKW" // AES-GCM key wrap (256)
  18. A256KW KeyEncryptionAlgorithm = "A256KW" // AES key wrap (256)
  19. DIRECT KeyEncryptionAlgorithm = "dir" // Direct encryption
  20. ECDH_ES KeyEncryptionAlgorithm = "ECDH-ES" // ECDH-ES
  21. ECDH_ES_A128KW KeyEncryptionAlgorithm = "ECDH-ES+A128KW" // ECDH-ES + AES key wrap (128)
  22. ECDH_ES_A192KW KeyEncryptionAlgorithm = "ECDH-ES+A192KW" // ECDH-ES + AES key wrap (192)
  23. ECDH_ES_A256KW KeyEncryptionAlgorithm = "ECDH-ES+A256KW" // ECDH-ES + AES key wrap (256)
  24. PBES2_HS256_A128KW KeyEncryptionAlgorithm = "PBES2-HS256+A128KW" // PBES2 + HMAC-SHA256 + AES key wrap (128)
  25. PBES2_HS384_A192KW KeyEncryptionAlgorithm = "PBES2-HS384+A192KW" // PBES2 + HMAC-SHA384 + AES key wrap (192)
  26. PBES2_HS512_A256KW KeyEncryptionAlgorithm = "PBES2-HS512+A256KW" // PBES2 + HMAC-SHA512 + AES key wrap (256)
  27. RSA1_5 KeyEncryptionAlgorithm = "RSA1_5" // RSA-PKCS1v1.5
  28. RSA_OAEP KeyEncryptionAlgorithm = "RSA-OAEP" // RSA-OAEP-SHA1
  29. RSA_OAEP_256 KeyEncryptionAlgorithm = "RSA-OAEP-256" // RSA-OAEP-SHA256
  30. )
  31. var allKeyEncryptionAlgorithms = map[KeyEncryptionAlgorithm]struct{}{
  32. A128GCMKW: {},
  33. A128KW: {},
  34. A192GCMKW: {},
  35. A192KW: {},
  36. A256GCMKW: {},
  37. A256KW: {},
  38. DIRECT: {},
  39. ECDH_ES: {},
  40. ECDH_ES_A128KW: {},
  41. ECDH_ES_A192KW: {},
  42. ECDH_ES_A256KW: {},
  43. PBES2_HS256_A128KW: {},
  44. PBES2_HS384_A192KW: {},
  45. PBES2_HS512_A256KW: {},
  46. RSA1_5: {},
  47. RSA_OAEP: {},
  48. RSA_OAEP_256: {},
  49. }
  50. var listKeyEncryptionAlgorithmOnce sync.Once
  51. var listKeyEncryptionAlgorithm []KeyEncryptionAlgorithm
  52. // KeyEncryptionAlgorithms returns a list of all available values for KeyEncryptionAlgorithm
  53. func KeyEncryptionAlgorithms() []KeyEncryptionAlgorithm {
  54. listKeyEncryptionAlgorithmOnce.Do(func() {
  55. listKeyEncryptionAlgorithm = make([]KeyEncryptionAlgorithm, 0, len(allKeyEncryptionAlgorithms))
  56. for v := range allKeyEncryptionAlgorithms {
  57. listKeyEncryptionAlgorithm = append(listKeyEncryptionAlgorithm, v)
  58. }
  59. sort.Slice(listKeyEncryptionAlgorithm, func(i, j int) bool {
  60. return string(listKeyEncryptionAlgorithm[i]) < string(listKeyEncryptionAlgorithm[j])
  61. })
  62. })
  63. return listKeyEncryptionAlgorithm
  64. }
  65. // Accept is used when conversion from values given by
  66. // outside sources (such as JSON payloads) is required
  67. func (v *KeyEncryptionAlgorithm) Accept(value interface{}) error {
  68. var tmp KeyEncryptionAlgorithm
  69. if x, ok := value.(KeyEncryptionAlgorithm); ok {
  70. tmp = x
  71. } else {
  72. var s string
  73. switch x := value.(type) {
  74. case fmt.Stringer:
  75. s = x.String()
  76. case string:
  77. s = x
  78. default:
  79. return errors.Errorf(`invalid type for jwa.KeyEncryptionAlgorithm: %T`, value)
  80. }
  81. tmp = KeyEncryptionAlgorithm(s)
  82. }
  83. if _, ok := allKeyEncryptionAlgorithms[tmp]; !ok {
  84. return errors.Errorf(`invalid jwa.KeyEncryptionAlgorithm value`)
  85. }
  86. *v = tmp
  87. return nil
  88. }
  89. // String returns the string representation of a KeyEncryptionAlgorithm
  90. func (v KeyEncryptionAlgorithm) String() string {
  91. return string(v)
  92. }
  93. // IsSymmetric returns true if the algorithm is a symmetric type
  94. func (v KeyEncryptionAlgorithm) IsSymmetric() bool {
  95. switch v {
  96. case A128GCMKW, A128KW, A192GCMKW, A192KW, A256GCMKW, A256KW, DIRECT, PBES2_HS256_A128KW, PBES2_HS384_A192KW, PBES2_HS512_A256KW:
  97. return true
  98. }
  99. return false
  100. }