KerberosFlags.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package types
  2. // Reference: https://www.ietf.org/rfc/rfc4120.txt
  3. // Section: 5.2.8
  4. import (
  5. "github.com/jcmturner/gofork/encoding/asn1"
  6. )
  7. // NewKrbFlags returns an ASN1 BitString struct of the right size for KrbFlags.
  8. func NewKrbFlags() asn1.BitString {
  9. f := asn1.BitString{}
  10. f.Bytes = make([]byte, 4)
  11. f.BitLength = len(f.Bytes) * 8
  12. return f
  13. }
  14. // SetFlags sets the flags of an ASN1 BitString.
  15. func SetFlags(f *asn1.BitString, j []int) {
  16. for _, i := range j {
  17. SetFlag(f, i)
  18. }
  19. }
  20. // SetFlag sets a flag in an ASN1 BitString.
  21. func SetFlag(f *asn1.BitString, i int) {
  22. for l := len(f.Bytes); l < 4; l++ {
  23. (*f).Bytes = append((*f).Bytes, byte(0))
  24. (*f).BitLength = len((*f).Bytes) * 8
  25. }
  26. //Which byte?
  27. b := i / 8
  28. //Which bit in byte
  29. p := uint(7 - (i - 8*b))
  30. (*f).Bytes[b] = (*f).Bytes[b] | (1 << p)
  31. }
  32. // UnsetFlags unsets flags in an ASN1 BitString.
  33. func UnsetFlags(f *asn1.BitString, j []int) {
  34. for _, i := range j {
  35. UnsetFlag(f, i)
  36. }
  37. }
  38. // UnsetFlag unsets a flag in an ASN1 BitString.
  39. func UnsetFlag(f *asn1.BitString, i int) {
  40. for l := len(f.Bytes); l < 4; l++ {
  41. (*f).Bytes = append((*f).Bytes, byte(0))
  42. (*f).BitLength = len((*f).Bytes) * 8
  43. }
  44. //Which byte?
  45. b := i / 8
  46. //Which bit in byte
  47. p := uint(7 - (i - 8*b))
  48. (*f).Bytes[b] = (*f).Bytes[b] &^ (1 << p)
  49. }
  50. // IsFlagSet tests if a flag is set in the ASN1 BitString.
  51. func IsFlagSet(f *asn1.BitString, i int) bool {
  52. //Which byte?
  53. b := i / 8
  54. //Which bit in byte
  55. p := uint(7 - (i - 8*b))
  56. if (*f).Bytes[b]&(1<<p) != 0 {
  57. return true
  58. }
  59. return false
  60. }