github.com/lestrrat-go/jwx/v2@v2.0.21/jwa/content_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  // ContentEncryptionAlgorithm represents the various encryption algorithms as described in https://tools.ietf.org/html/rfc7518#section-5
    12  type ContentEncryptionAlgorithm string
    13  
    14  // Supported values for ContentEncryptionAlgorithm
    15  const (
    16  	A128CBC_HS256 ContentEncryptionAlgorithm = "A128CBC-HS256" // AES-CBC + HMAC-SHA256 (128)
    17  	A128GCM       ContentEncryptionAlgorithm = "A128GCM"       // AES-GCM (128)
    18  	A192CBC_HS384 ContentEncryptionAlgorithm = "A192CBC-HS384" // AES-CBC + HMAC-SHA384 (192)
    19  	A192GCM       ContentEncryptionAlgorithm = "A192GCM"       // AES-GCM (192)
    20  	A256CBC_HS512 ContentEncryptionAlgorithm = "A256CBC-HS512" // AES-CBC + HMAC-SHA512 (256)
    21  	A256GCM       ContentEncryptionAlgorithm = "A256GCM"       // AES-GCM (256)
    22  )
    23  
    24  var muContentEncryptionAlgorithms sync.RWMutex
    25  var allContentEncryptionAlgorithms map[ContentEncryptionAlgorithm]struct{}
    26  var listContentEncryptionAlgorithm []ContentEncryptionAlgorithm
    27  
    28  func init() {
    29  	muContentEncryptionAlgorithms.Lock()
    30  	defer muContentEncryptionAlgorithms.Unlock()
    31  	allContentEncryptionAlgorithms = make(map[ContentEncryptionAlgorithm]struct{})
    32  	allContentEncryptionAlgorithms[A128CBC_HS256] = struct{}{}
    33  	allContentEncryptionAlgorithms[A128GCM] = struct{}{}
    34  	allContentEncryptionAlgorithms[A192CBC_HS384] = struct{}{}
    35  	allContentEncryptionAlgorithms[A192GCM] = struct{}{}
    36  	allContentEncryptionAlgorithms[A256CBC_HS512] = struct{}{}
    37  	allContentEncryptionAlgorithms[A256GCM] = struct{}{}
    38  	rebuildContentEncryptionAlgorithm()
    39  }
    40  
    41  // RegisterContentEncryptionAlgorithm registers a new ContentEncryptionAlgorithm so that the jwx can properly handle the new value.
    42  // Duplicates will silently be ignored
    43  func RegisterContentEncryptionAlgorithm(v ContentEncryptionAlgorithm) {
    44  	muContentEncryptionAlgorithms.Lock()
    45  	defer muContentEncryptionAlgorithms.Unlock()
    46  	if _, ok := allContentEncryptionAlgorithms[v]; !ok {
    47  		allContentEncryptionAlgorithms[v] = struct{}{}
    48  		rebuildContentEncryptionAlgorithm()
    49  	}
    50  }
    51  
    52  // UnregisterContentEncryptionAlgorithm unregisters a ContentEncryptionAlgorithm from its known database.
    53  // Non-existentn entries will silently be ignored
    54  func UnregisterContentEncryptionAlgorithm(v ContentEncryptionAlgorithm) {
    55  	muContentEncryptionAlgorithms.Lock()
    56  	defer muContentEncryptionAlgorithms.Unlock()
    57  	if _, ok := allContentEncryptionAlgorithms[v]; ok {
    58  		delete(allContentEncryptionAlgorithms, v)
    59  		rebuildContentEncryptionAlgorithm()
    60  	}
    61  }
    62  
    63  func rebuildContentEncryptionAlgorithm() {
    64  	listContentEncryptionAlgorithm = make([]ContentEncryptionAlgorithm, 0, len(allContentEncryptionAlgorithms))
    65  	for v := range allContentEncryptionAlgorithms {
    66  		listContentEncryptionAlgorithm = append(listContentEncryptionAlgorithm, v)
    67  	}
    68  	sort.Slice(listContentEncryptionAlgorithm, func(i, j int) bool {
    69  		return string(listContentEncryptionAlgorithm[i]) < string(listContentEncryptionAlgorithm[j])
    70  	})
    71  }
    72  
    73  // ContentEncryptionAlgorithms returns a list of all available values for ContentEncryptionAlgorithm
    74  func ContentEncryptionAlgorithms() []ContentEncryptionAlgorithm {
    75  	muContentEncryptionAlgorithms.RLock()
    76  	defer muContentEncryptionAlgorithms.RUnlock()
    77  	return listContentEncryptionAlgorithm
    78  }
    79  
    80  // Accept is used when conversion from values given by
    81  // outside sources (such as JSON payloads) is required
    82  func (v *ContentEncryptionAlgorithm) Accept(value interface{}) error {
    83  	var tmp ContentEncryptionAlgorithm
    84  	if x, ok := value.(ContentEncryptionAlgorithm); ok {
    85  		tmp = x
    86  	} else {
    87  		var s string
    88  		switch x := value.(type) {
    89  		case fmt.Stringer:
    90  			s = x.String()
    91  		case string:
    92  			s = x
    93  		default:
    94  			return fmt.Errorf(`invalid type for jwa.ContentEncryptionAlgorithm: %T`, value)
    95  		}
    96  		tmp = ContentEncryptionAlgorithm(s)
    97  	}
    98  	if _, ok := allContentEncryptionAlgorithms[tmp]; !ok {
    99  		return fmt.Errorf(`invalid jwa.ContentEncryptionAlgorithm value`)
   100  	}
   101  
   102  	*v = tmp
   103  	return nil
   104  }
   105  
   106  // String returns the string representation of a ContentEncryptionAlgorithm
   107  func (v ContentEncryptionAlgorithm) String() string {
   108  	return string(v)
   109  }