github.com/lestrrat-go/jwx/v2@v2.0.21/jwa/key_encryption_gen.go (about)

     1  // Code generated by tools/cmd/genjwa/main.go. DO NOT EDIT.
     2  
     3  package jwa
     4  
     5  import (
     6  	"fmt"
     7  	"sort"
     8  	"sync"
     9  )
    10  
    11  // KeyEncryptionAlgorithm represents the various encryption algorithms as described in https://tools.ietf.org/html/rfc7518#section-4.1
    12  type KeyEncryptionAlgorithm string
    13  
    14  // Supported values for KeyEncryptionAlgorithm
    15  const (
    16  	A128GCMKW          KeyEncryptionAlgorithm = "A128GCMKW"          // AES-GCM key wrap (128)
    17  	A128KW             KeyEncryptionAlgorithm = "A128KW"             // AES key wrap (128)
    18  	A192GCMKW          KeyEncryptionAlgorithm = "A192GCMKW"          // AES-GCM key wrap (192)
    19  	A192KW             KeyEncryptionAlgorithm = "A192KW"             // AES key wrap (192)
    20  	A256GCMKW          KeyEncryptionAlgorithm = "A256GCMKW"          // AES-GCM key wrap (256)
    21  	A256KW             KeyEncryptionAlgorithm = "A256KW"             // AES key wrap (256)
    22  	DIRECT             KeyEncryptionAlgorithm = "dir"                // Direct encryption
    23  	ECDH_ES            KeyEncryptionAlgorithm = "ECDH-ES"            // ECDH-ES
    24  	ECDH_ES_A128KW     KeyEncryptionAlgorithm = "ECDH-ES+A128KW"     // ECDH-ES + AES key wrap (128)
    25  	ECDH_ES_A192KW     KeyEncryptionAlgorithm = "ECDH-ES+A192KW"     // ECDH-ES + AES key wrap (192)
    26  	ECDH_ES_A256KW     KeyEncryptionAlgorithm = "ECDH-ES+A256KW"     // ECDH-ES + AES key wrap (256)
    27  	PBES2_HS256_A128KW KeyEncryptionAlgorithm = "PBES2-HS256+A128KW" // PBES2 + HMAC-SHA256 + AES key wrap (128)
    28  	PBES2_HS384_A192KW KeyEncryptionAlgorithm = "PBES2-HS384+A192KW" // PBES2 + HMAC-SHA384 + AES key wrap (192)
    29  	PBES2_HS512_A256KW KeyEncryptionAlgorithm = "PBES2-HS512+A256KW" // PBES2 + HMAC-SHA512 + AES key wrap (256)
    30  	RSA1_5             KeyEncryptionAlgorithm = "RSA1_5"             // RSA-PKCS1v1.5
    31  	RSA_OAEP           KeyEncryptionAlgorithm = "RSA-OAEP"           // RSA-OAEP-SHA1
    32  	RSA_OAEP_256       KeyEncryptionAlgorithm = "RSA-OAEP-256"       // RSA-OAEP-SHA256
    33  )
    34  
    35  var muKeyEncryptionAlgorithms sync.RWMutex
    36  var allKeyEncryptionAlgorithms map[KeyEncryptionAlgorithm]struct{}
    37  var listKeyEncryptionAlgorithm []KeyEncryptionAlgorithm
    38  
    39  func init() {
    40  	muKeyEncryptionAlgorithms.Lock()
    41  	defer muKeyEncryptionAlgorithms.Unlock()
    42  	allKeyEncryptionAlgorithms = make(map[KeyEncryptionAlgorithm]struct{})
    43  	allKeyEncryptionAlgorithms[A128GCMKW] = struct{}{}
    44  	allKeyEncryptionAlgorithms[A128KW] = struct{}{}
    45  	allKeyEncryptionAlgorithms[A192GCMKW] = struct{}{}
    46  	allKeyEncryptionAlgorithms[A192KW] = struct{}{}
    47  	allKeyEncryptionAlgorithms[A256GCMKW] = struct{}{}
    48  	allKeyEncryptionAlgorithms[A256KW] = struct{}{}
    49  	allKeyEncryptionAlgorithms[DIRECT] = struct{}{}
    50  	allKeyEncryptionAlgorithms[ECDH_ES] = struct{}{}
    51  	allKeyEncryptionAlgorithms[ECDH_ES_A128KW] = struct{}{}
    52  	allKeyEncryptionAlgorithms[ECDH_ES_A192KW] = struct{}{}
    53  	allKeyEncryptionAlgorithms[ECDH_ES_A256KW] = struct{}{}
    54  	allKeyEncryptionAlgorithms[PBES2_HS256_A128KW] = struct{}{}
    55  	allKeyEncryptionAlgorithms[PBES2_HS384_A192KW] = struct{}{}
    56  	allKeyEncryptionAlgorithms[PBES2_HS512_A256KW] = struct{}{}
    57  	allKeyEncryptionAlgorithms[RSA1_5] = struct{}{}
    58  	allKeyEncryptionAlgorithms[RSA_OAEP] = struct{}{}
    59  	allKeyEncryptionAlgorithms[RSA_OAEP_256] = struct{}{}
    60  	rebuildKeyEncryptionAlgorithm()
    61  }
    62  
    63  // RegisterKeyEncryptionAlgorithm registers a new KeyEncryptionAlgorithm so that the jwx can properly handle the new value.
    64  // Duplicates will silently be ignored
    65  func RegisterKeyEncryptionAlgorithm(v KeyEncryptionAlgorithm) {
    66  	muKeyEncryptionAlgorithms.Lock()
    67  	defer muKeyEncryptionAlgorithms.Unlock()
    68  	if _, ok := allKeyEncryptionAlgorithms[v]; !ok {
    69  		allKeyEncryptionAlgorithms[v] = struct{}{}
    70  		rebuildKeyEncryptionAlgorithm()
    71  	}
    72  }
    73  
    74  // UnregisterKeyEncryptionAlgorithm unregisters a KeyEncryptionAlgorithm from its known database.
    75  // Non-existentn entries will silently be ignored
    76  func UnregisterKeyEncryptionAlgorithm(v KeyEncryptionAlgorithm) {
    77  	muKeyEncryptionAlgorithms.Lock()
    78  	defer muKeyEncryptionAlgorithms.Unlock()
    79  	if _, ok := allKeyEncryptionAlgorithms[v]; ok {
    80  		delete(allKeyEncryptionAlgorithms, v)
    81  		rebuildKeyEncryptionAlgorithm()
    82  	}
    83  }
    84  
    85  func rebuildKeyEncryptionAlgorithm() {
    86  	listKeyEncryptionAlgorithm = make([]KeyEncryptionAlgorithm, 0, len(allKeyEncryptionAlgorithms))
    87  	for v := range allKeyEncryptionAlgorithms {
    88  		listKeyEncryptionAlgorithm = append(listKeyEncryptionAlgorithm, v)
    89  	}
    90  	sort.Slice(listKeyEncryptionAlgorithm, func(i, j int) bool {
    91  		return string(listKeyEncryptionAlgorithm[i]) < string(listKeyEncryptionAlgorithm[j])
    92  	})
    93  }
    94  
    95  // KeyEncryptionAlgorithms returns a list of all available values for KeyEncryptionAlgorithm
    96  func KeyEncryptionAlgorithms() []KeyEncryptionAlgorithm {
    97  	muKeyEncryptionAlgorithms.RLock()
    98  	defer muKeyEncryptionAlgorithms.RUnlock()
    99  	return listKeyEncryptionAlgorithm
   100  }
   101  
   102  // Accept is used when conversion from values given by
   103  // outside sources (such as JSON payloads) is required
   104  func (v *KeyEncryptionAlgorithm) Accept(value interface{}) error {
   105  	var tmp KeyEncryptionAlgorithm
   106  	if x, ok := value.(KeyEncryptionAlgorithm); ok {
   107  		tmp = x
   108  	} else {
   109  		var s string
   110  		switch x := value.(type) {
   111  		case fmt.Stringer:
   112  			s = x.String()
   113  		case string:
   114  			s = x
   115  		default:
   116  			return fmt.Errorf(`invalid type for jwa.KeyEncryptionAlgorithm: %T`, value)
   117  		}
   118  		tmp = KeyEncryptionAlgorithm(s)
   119  	}
   120  	if _, ok := allKeyEncryptionAlgorithms[tmp]; !ok {
   121  		return fmt.Errorf(`invalid jwa.KeyEncryptionAlgorithm value`)
   122  	}
   123  
   124  	*v = tmp
   125  	return nil
   126  }
   127  
   128  // String returns the string representation of a KeyEncryptionAlgorithm
   129  func (v KeyEncryptionAlgorithm) String() string {
   130  	return string(v)
   131  }
   132  
   133  // IsSymmetric returns true if the algorithm is a symmetric type
   134  func (v KeyEncryptionAlgorithm) IsSymmetric() bool {
   135  	switch v {
   136  	case A128GCMKW, A128KW, A192GCMKW, A192KW, A256GCMKW, A256KW, DIRECT, PBES2_HS256_A128KW, PBES2_HS384_A192KW, PBES2_HS512_A256KW:
   137  		return true
   138  	}
   139  	return false
   140  }