objectid

package module
v1.0.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 13, 2024 License: MIT Imports: 6 Imported by: 1

README

go-objectid

Go Report Card OpenSSF Best Practices CodeQL Go Reference Software License codecov contributions welcome GitHub Workflow Status (with event) GitHub release (with filter) Author Help Animals

Package objectid offers convenient ASN.1 Object Identifier types with useful methods and an unbounded NumberForm type allowing support for ITU-T Rec. X.667 compliant ASN.1 Object Identifiers and beyond!

Documentation

Overview

Package objectid implements ASN.1 Object Identifier types and methods.

Features

  • Unbounded NumberForm support
  • ASN.1 encoding and decoding of DotNotation instances -- without use of the encoding/asn1 package
  • Flexible index support, allowing interrogation through negative indices without the risk of panic
  • Convenient Leaf, Parent and Root index alias methods, wherever applicable
  • Ge, Gt, Le, Lt, Equal comparison methods for interacting with NumberForm instances
  • Conversion friendly -- easy hand-off to encoding/asn1.ObjectIdentifier and crypto/x509.OID instances

License

The go-objectid package is available under the terms of the MIT license.

For further details, see the LICENSE file within the root of the source repository.

Boundless NumberForm

Instances of the NumberForm type are subject to no magnitude limits. This means that any given NumberForm may be set to any number, provided it is unsigned, regardless of its size. In particular, this is necessary to support ITU-T Rec. X.667 OIDs, which are UUID based and require 128-bit unsigned integer support.

Be aware that the NumberForm type is based upon math/big.Int. Therefore, given sufficiently large values, performance penalties may appear during routine operations. Keep in mind that most OIDs do not bear such large values.

ASN.1 Codec

The ASN.1 encoding and decoding scheme implemented in this package does not use the encoding/asn1 package, nor does it utilize any 3rd party ASN.1 implementation. It is a custom, bidirectional implementation.

ASN.1 codec features are written solely for DotNotation related cases (e.g.: "1.3.6.1.4.1.56521"), and allow the following:

  • Encoding (marshaling) of a DotNotation into an ASN.1 encoded value ([]byte{...})
  • Decoding (unmarshaling) of encoded values into an unpopulated DotNotation instance

Encoding of non-minimal values -- such as root arcs "0", "1" and "2" alone -- is not supported. Some ASN.1 implementations precariously treat certain OIDs, such as "0" and "0.0" the same, likely for support reasons. This results in ambiguity when handling pre-encoded bytes in an obverse scenario, and is in violation of ITU-T Rec. X.690 regarding the proper encoding of an ASN.1 OBJECT IDENTIFIER.

In short, codec functions will only operate successfully when given DotNotation comprised of two (2) or more NumberForm instances.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ASN1Notation

type ASN1Notation []NameAndNumberForm

ASN1Notation contains an ordered sequence of NameAndNumberForm instances.

func NewASN1Notation

func NewASN1Notation(x any) (r *ASN1Notation, err error)

NewASN1Notation returns an instance of *ASN1Notation alongside an error.

Valid input forms for ASN.1 values are:

Note that the following root node abbreviations are supported:

  • `itu-t` resolves to itu-t(0)
  • `iso` resolves to iso(1)
  • `joint-iso-itu-t` resolves to joint-iso-itu-t(2)

Case is significant during processing of the above abbreviations. Note that it is inappropriate to utilize these abbreviations for any portion of an ASN1Notation instance other than as the respective root node.

NumberForm values CANNOT be negative, but are unbounded in their magnitude.

Example
a := `{iso identified-organization(3) dod(6)}`
id, err := NewASN1Notation(a)
if err != nil {
	fmt.Println(err)
	return
}
fmt.Printf("Root node: %s", id.Root())
Output:

Root node: iso(1)

func (ASN1Notation) AncestorOf

func (r ASN1Notation) AncestorOf(asn any) (anc bool)

AncestorOf returns a Boolean value indicative of whether the receiver is an ancestor of the input value, which can be string or ASN1Notation.

Example
asn, _ := NewASN1Notation(`{iso(1) identified-organization(3) dod(6) internet(1)}`)
child, _ := NewASN1Notation(`{iso(1) identified-organization(3) dod(6) internet(1) private(4) enterprise(1)}`)
fmt.Printf("%t", asn.AncestorOf(child))
Output:

true

func (ASN1Notation) Ancestry

func (r ASN1Notation) Ancestry() (anc []ASN1Notation)

Ancestry returns slices of DotNotation values ordered from leaf node (first) to root node (last).

Empty slices of DotNotation are returned if the dotNotation value within the receiver is less than two (2) NumberForm values in length.

Example
asn, err := NewASN1Notation(`{iso(1) identified-organization(3) dod(6) internet(1) private(4) enterprise(1) 56521 example(999)}`)
if err != nil {
	fmt.Println(err)
}

anc := asn.Ancestry()
fmt.Printf("%s", anc[len(anc)-2])
Output:

{iso(1) identified-organization(3)}

func (ASN1Notation) Dot added in v1.0.4

func (r ASN1Notation) Dot() (d DotNotation)

Dot returns a DotNotation instance based on the contents of the receiver instance.

Note that at a receiver length of two (2) or more is required for successful output.

Example
aNot, err := NewASN1Notation(`{iso(1) identified-organization(3) dod(6) internet(1) private(4) enterprise(1) 56521 example(999)}`)
if err != nil {
	fmt.Println(err)
	return
}
dot := aNot.Dot()
fmt.Println(dot)
Output:

1.3.6.1.4.1.56521.999
Example (Bogus)

This example demonstrates a bogus DotNotation output due to the presence of less than two (2) NameAndNumberForm instances within the receiver.

DotNotation ALWAYS requires two (2) or more elements to be considered valid.

aNot, err := NewASN1Notation(`{iso(1)}`)
if err != nil {
	fmt.Println(err)
	return
}
dot := aNot.Dot()
fmt.Println(dot)
Output:

func (ASN1Notation) Index

func (r ASN1Notation) Index(idx int) (nanf NameAndNumberForm, ok bool)

Index returns the Nth index from the receiver, alongside a Boolean value indicative of success. This method supports the use of negative indices.

Example
aNot, err := NewASN1Notation(`{iso(1) identified-organization(3) dod(6) internet(1) private(4) enterprise(1) 56521 example(999)}`)
if err != nil {
	fmt.Println(err)
	return
}

nanf, _ := aNot.Index(1)
fmt.Printf("%s", nanf)
Output:

identified-organization(3)

func (ASN1Notation) IsZero

func (r ASN1Notation) IsZero() (is bool)

IsZero returns a Boolean indicative of whether the receiver is unset.

Example
var aNot ASN1Notation
fmt.Printf("Is Zero: %t", aNot.IsZero())
Output:

Is Zero: true

func (ASN1Notation) Leaf

Leaf returns the leaf node (-1) string value from the receiver.

Example
aNot, err := NewASN1Notation(`{iso(1) identified-organization(3) dod(6) internet(1) private(4) enterprise(1) 56521 example(999)}`)
if err != nil {
	fmt.Println(err)
	return
}

fmt.Printf("%s", aNot.Leaf())
Output:

example(999)

func (ASN1Notation) Len

func (r ASN1Notation) Len() int

Len returns the integer length of the receiver.

Example
aNot, err := NewASN1Notation(`{iso(1) identified-organization(3) dod(6) internet(1) private(4) enterprise(1) 56521 example(999)}`)
if err != nil {
	fmt.Println(err)
	return
}

fmt.Printf("Length: %d", aNot.Len())
Output:

Length: 8

func (ASN1Notation) NewSubordinate

func (r ASN1Notation) NewSubordinate(nanf any) *ASN1Notation

NewSubordinate returns a new instance of ASN1Notation based upon the contents of the receiver as well as the input NameAndNumberForm subordinate value. This creates a fully-qualified child ASN1Notation value of the receiver.

Example
asn, err := NewASN1Notation(`{iso(1) identified-organization(3) dod(6) internet(1) private(4) enterprise(1) 56521 example(999)}`)
if err != nil {
	fmt.Println(err)
}

fmt.Printf("%s", asn.NewSubordinate(`friedChicken(5)`))
Output:

{iso(1) identified-organization(3) dod(6) internet(1) private(4) enterprise(1) 56521 example(999) friedChicken(5)}

func (ASN1Notation) Parent

func (r ASN1Notation) Parent() NameAndNumberForm

Parent returns the leaf node's parent (-2) string value from the receiver.

Example
aNot, err := NewASN1Notation(`{iso(1) identified-organization(3) dod(6) internet(1) private(4) enterprise(1) 56521 example(999)}`)
if err != nil {
	fmt.Println(err)
	return
}

fmt.Printf("%s", aNot.Parent())
Output:

56521

func (ASN1Notation) Root

Root returns the root node (0) string value from the receiver.

Example
aNot, err := NewASN1Notation(`{iso(1) identified-organization(3) dod(6) internet(1) private(4) enterprise(1) 56521 example(999)}`)
if err != nil {
	fmt.Println(err)
	return
}

fmt.Printf("%s", aNot.Root())
Output:

iso(1)

func (ASN1Notation) String

func (r ASN1Notation) String() string

String is a stringer method that returns a properly formatted ASN.1 string value.

Example
aNot, err := NewASN1Notation(`{iso(1) identified-organization(3) dod(6) internet(1) private(4) enterprise(1) 56521 example(999)}`)
if err != nil {
	fmt.Println(err)
	return
}

fmt.Printf("%s", aNot)
Output:

{iso(1) identified-organization(3) dod(6) internet(1) private(4) enterprise(1) 56521 example(999)}

func (ASN1Notation) Valid

func (r ASN1Notation) Valid() (is bool)

Valid returns a Boolean value indicative of whether the receiver's length is greater than or equal to one (1) slice member.

Example
var aNot ASN1Notation
fmt.Printf("Is Valid: %t", aNot.Valid())
Output:

Is Valid: false

type DotNotation

type DotNotation []NumberForm

DotNotation contains an ordered sequence of NumberForm instances.

func NewDotNotation

func NewDotNotation(x ...any) (r *DotNotation, err error)

NewDotNotation returns an instance of *DotNotation alongside a Boolean value indicative of success.

Variadic input allows for slice mixtures of all of the following types, each treated as an individual NumberForm instance:

If a string primitive is the only input option, it will be treated as a complete DotNotation (e.g.: "1.3.6").

Example
a := `2.25.987895962269883002155146617097157934`
id, err := NewDotNotation(a)
if err != nil {
	fmt.Println(err)
	return
}
fmt.Printf("dotNotation: %s", id)
Output:

dotNotation: 2.25.987895962269883002155146617097157934
Example (MixedVariadicInput)

This example demonstrates a means of creating a new instance of DotNotation using variadic input comprised of mixed (supported) type instances.

This may be useful in cases where an instance of DotNotation is being created using relative components derived elsewhere, or otherwise inferred incrementally in some manner.

dot, err := NewDotNotation(uint(1), uint(3), big.NewInt(6), `1`, `4`, `1`, uint64(56521), 999, 5)
if err != nil {
	fmt.Println(err)
	return
}
fmt.Printf("%s", dot)
Output:

1.3.6.1.4.1.56521.999.5

func (DotNotation) AncestorOf

func (r DotNotation) AncestorOf(dot any) (is bool)

AncestorOf returns a Boolean value indicative of whether the receiver is an ancestor of the input value, which can be string or DotNotation.

Example
dot, _ := NewDotNotation(`1.3.6`)
child, _ := NewDotNotation(`2.1.0.1`)
fmt.Printf("%t", dot.AncestorOf(child))
Output:

false

func (DotNotation) Ancestry

func (r DotNotation) Ancestry() (anc []DotNotation)

Ancestry returns slices of DotNotation values ordered from leaf node (first) to root node (last).

Empty slices of DotNotation are returned if the dot notation value within the receiver is less than two (2) NumberForm values in length.

Example
dot, err := NewDotNotation(`1.3.6.1.4.1.56521`)
if err != nil {
	fmt.Println(err)
}

anc := dot.Ancestry()
fmt.Printf("%s", anc[len(anc)-2])
Output:

1.3

func (*DotNotation) Decode added in v1.0.4

func (r *DotNotation) Decode(b []byte) (err error)

Decode returns an error following an attempt to parse b, which must be the ASN.1 encoding of an OID, into the receiver instance. The receiver instance is reinitialized at runtime.

Example

This example demonstrates use of the DotNotation.Decode method, which returns an error following an attempt to decode b ([]byte), which is ASN.1 encoded, into the receiver instance.

The result should yield a freshly populated DotNotation receiver instance -- bearing the OID 1.3.6.1.4.1.56521.999.5 -- alongside a nil error.

var d DotNotation

// pre-encoded bytes for OID 1.3.6.1.4.1.56521.999.5
b := []byte{0x6, 0xb, 0x2b, 0x6, 0x1, 0x4, 0x1, 0x83, 0xb9, 0x49, 0x87, 0x67, 0x5}

err := d.Decode(b)
if err != nil {
	fmt.Println(err)
	return
}

fmt.Printf("%s\n", d)
Output:

1.3.6.1.4.1.56521.999.5

func (DotNotation) Encode added in v1.0.4

func (r DotNotation) Encode() (b []byte, err error)

Encode returns the ASN.1 encoding of the receiver instance alongside an error.

Example

This example demonstrates use of the DotNotation.Encode method, which returns the ASN.1 encoding of the DotNotation instance alongside error.

The result should yield a complete ASN.1 byte sequence, representing the encoded form of the receiver instance of DotNotation.

dot, _ := NewDotNotation(`1.3.6.1.4.1.56521.999.5`)
b, err := dot.Encode()
if err != nil {
	fmt.Println(err)
	return
}

fmt.Printf("%v", b)
Output:

[6 11 43 6 1 4 1 131 185 73 135 103 5]

func (DotNotation) Index

func (r DotNotation) Index(idx int) (a NumberForm, ok bool)

Index returns the Nth index from the receiver, alongside a Boolean value indicative of success. This method supports the use of negative indices.

Example
dot, err := NewDotNotation(`1.3.6.1.4.1.56521.999.5`)
if err != nil {
	fmt.Println(err)
	return
}

arc, _ := dot.Index(1)
fmt.Printf("%s", arc)
Output:

3

func (DotNotation) IntSlice

func (r DotNotation) IntSlice() (slice []int, err error)

IntSlice returns slices of integer values and an error. The integer values are based upon the contents of the receiver. Note that if any single arc number overflows int, a zero slice is returned.

Successful output can be cast as an instance of encoding/asn1.ObjectIdentifier, if desired.

Example
a := `1.3.6.1.4.1.56521.999.5`
dot, _ := NewDotNotation(a)

// If needed, slice instance can be
// cast as asn1.ObjectIdentifier.
slice, err := dot.IntSlice()
if err != nil {
	fmt.Println(err)
	return
}
fmt.Printf("%v", slice)
Output:

[1 3 6 1 4 1 56521 999 5]
Example (Overflow)
a := `2.25.987895962269883002155146617097157934`
dot, _ := NewDotNotation(a)
if _, err := dot.IntSlice(); err != nil {
	fmt.Println(err)
	return
}
Output:

strconv.Atoi: parsing "987895962269883002155146617097157934": value out of range

func (*DotNotation) IsZero

func (r *DotNotation) IsZero() (is bool)

IsZero returns a Boolean indicative of whether the receiver is unset.

Example
var dot DotNotation
fmt.Printf("Is Zero: %t", dot.IsZero())
Output:

Is Zero: true

func (DotNotation) Leaf

func (r DotNotation) Leaf() NumberForm

Leaf returns the leaf-node (-1) NumberForm instance.

Example
a := `2.25.987895962269883002155146617097157934`
id, err := NewDotNotation(a)
if err != nil {
	fmt.Println(err)
	return
}

fmt.Printf("Leaf node: %s", id.Leaf())
Output:

Leaf node: 987895962269883002155146617097157934

func (DotNotation) Len

func (r DotNotation) Len() int
Example
dot, err := NewDotNotation(`1.3.6.1.4.1.56521.999.5`)
if err != nil {
	fmt.Println(err)
	return
}

fmt.Printf("Length: %d", dot.Len())
Output:

Length: 9

func (DotNotation) NewSubordinate

func (r DotNotation) NewSubordinate(nf any) (dot *DotNotation)

NewSubordinate returns a new instance of DotNotation based upon the contents of the receiver as well as the input NumberForm subordinate value. This creates a fully-qualified child DotNotation value of the receiver.

Example
dot, err := NewDotNotation(`1.3.6.1.4.1.56521.999`)
if err != nil {
	fmt.Println(err)
}

fmt.Printf("%s", dot.NewSubordinate(5))
Output:

1.3.6.1.4.1.56521.999.5

func (DotNotation) Parent

func (r DotNotation) Parent() NumberForm

Parent returns the leaf-node's parent (-2) NumberForm instance.

Example
a := `2.25.987895962269883002155146617097157934`
id, err := NewDotNotation(a)
if err != nil {
	fmt.Println(err)
	return
}

fmt.Printf("Leaf node parent: %s", id.Parent())
Output:

Leaf node parent: 25

func (DotNotation) Root

func (r DotNotation) Root() NumberForm

Root returns the root node (0) NumberForm instance.

Example
a := `2.25.987895962269883002155146617097157934`
id, err := NewDotNotation(a)
if err != nil {
	fmt.Println(err)
	return
}

fmt.Printf("Root node: %s", id.Root())
Output:

Root node: 2

func (DotNotation) String

func (r DotNotation) String() (s string)

String is a stringer method that returns the dot notation form of the receiver (e.g.: "1.3.6.1").

Example
dot, err := NewDotNotation(`1.3.6.1.4.1.56521.999.5`)
if err != nil {
	fmt.Println(err)
	return
}

fmt.Printf("%s", dot)
Output:

1.3.6.1.4.1.56521.999.5

func (DotNotation) Uint64Slice added in v1.0.4

func (r DotNotation) Uint64Slice() (slice []uint64, err error)

Uint64Slice returns slices of uint64 values and an error. The uint64 values are based upon the contents of the receiver.

Note that if any single arc number overflows uint64, a zero slice is returned alongside an error.

Successful output can be cast as an instance of crypto/x509.OID, if desired.

Example
a := `1.3.6.1.4.1.56521.9999999999999999999.5` // overflows int, but not uint64
dot, _ := NewDotNotation(a)

// If needed, slice instance can be
// fed to crypto/x509.OIDFromInts
slice, err := dot.Uint64Slice()
if err != nil {
	fmt.Println(err)
	return
}

fmt.Printf("%v", slice)
Output:

[1 3 6 1 4 1 56521 9999999999999999999 5]
Example (Overflow)
a := `2.25.987895962269883002155146617097157934`
dot, _ := NewDotNotation(a)
if _, err := dot.Uint64Slice(); err != nil {
	fmt.Println(err)
	return
}
Output:

strconv.ParseUint: parsing "987895962269883002155146617097157934": value out of range

func (DotNotation) Valid

func (r DotNotation) Valid() (is bool)

Valid returns a Boolean value indicative of the following:

  • Receiver's length is greater than or equal to two (2) slice members, AND ...
  • The first slice in the receiver contains an unsigned decimal value that is less than three (3)
Example
var dot DotNotation
fmt.Printf("Is Valid: %t", dot.Valid())
Output:

Is Valid: false

type NameAndNumberForm

type NameAndNumberForm struct {
	// contains filtered or unexported fields
}

NameAndNumberForm contains either an identifier with a parenthesis-enclosed decimal value, or a decimal value alone. An ordered sequence of instances of this type comprise an instance of ASN1Notation.

func NewNameAndNumberForm

func NewNameAndNumberForm(x any) (r *NameAndNumberForm, err error)

NewNameAndNumberForm returns an instance of *NameAndNumberForm alongside an error. Valid input forms are:

• nameAndNumberForm (e.g.: "enterprise(1)"), or ...

• numberForm (e.g.: 1)

NumberForm components CANNOT be negative. Permitted input types are string, uint, uint64, NumberForm, *math/big.Int and (non-negative) int.

Example
nanf, err := NewNameAndNumberForm(`enterprise(1)`)
if err != nil {
	fmt.Println(err)
	return
}

fmt.Printf("%s", nanf)
Output:

enterprise(1)

func (NameAndNumberForm) Equal

func (r NameAndNumberForm) Equal(n any) (is bool)

Equal returns a Boolean value indicative of whether instance n of NameAndNumberForm matches the receiver.

Example
var nanf1, nanf2 *NameAndNumberForm
var err error

if nanf1, err = NewNameAndNumberForm(`enterprise(1)`); err != nil {
	fmt.Println(err)
	return
}

// bogus
if nanf2, err = NewNameAndNumberForm(`enterprise(10)`); err != nil {
	fmt.Println(err)
	return
}

fmt.Printf("Equal: %t", nanf1.Equal(nanf2))
Output:

Equal: false

func (NameAndNumberForm) Identifier

func (r NameAndNumberForm) Identifier() string

Identifier returns the string-based nameForm value assigned to the receiver instance.

Example
nanf, err := NewNameAndNumberForm(`enterprise(1)`)
if err != nil {
	fmt.Println(err)
	return
}

fmt.Printf("%s", nanf.Identifier())
Output:

enterprise

func (NameAndNumberForm) IsZero

func (r NameAndNumberForm) IsZero() bool

IsZero returns a Boolean valu indicative of whether the receiver is considered nil.

Example
nanf, err := NewNameAndNumberForm(`enterprise(1)`)
if err != nil {
	fmt.Println(err)
	return
}

fmt.Printf("Zero: %t", nanf.IsZero())
Output:

Zero: false

func (NameAndNumberForm) NumberForm

func (r NameAndNumberForm) NumberForm() NumberForm

NumberForm returns the underlying NumberForm value assigned to the receiver instance.

Example
nanf, err := NewNameAndNumberForm(`enterprise(1)`)
if err != nil {
	fmt.Println(err)
	return
}

fmt.Printf("%s", nanf.NumberForm())
Output:

1

func (NameAndNumberForm) String

func (r NameAndNumberForm) String() (val string)

String is a stringer method that returns the properly formatted NameAndNumberForm string value.

Example
nanf, err := NewNameAndNumberForm(`enterprise(1)`)
if err != nil {
	fmt.Println(err)
	return
}

fmt.Printf("%s", nanf)
Output:

enterprise(1)

type NumberForm

type NumberForm big.Int

NumberForm is an unbounded, unsigned number.

func NewNumberForm

func NewNumberForm(v any) (r NumberForm, err error)

NewNumberForm converts v into an instance of NumberForm, which is returned alongside an error.

Valid input types are string, uint64, int, uint, and *math/big.Int.

Any input that represents a negative or unspecified number guarantees an error.

Example
// single UUID integer parse example
arc, err := NewNumberForm(`987895962269883002155146617097157934`)
if err != nil {
	fmt.Println(err)
	return
}
fmt.Printf("%s", arc)
Output:

987895962269883002155146617097157934

func (NumberForm) Equal

func (r NumberForm) Equal(n any) (is bool)

Equal returns a boolean value indicative of whether the receiver is equal to the value provided.

Valid input types are string, uint64, int, uint, *math/big.Int and NumberForm.

Any input that represents a negative or unspecified number guarantees a false return.

Example
nf1, _ := NewNumberForm(4658)
nf2, _ := NewNumberForm(4657)
fmt.Printf("Instances are equal: %t", nf1.Equal(nf2))
Output:

Instances are equal: false

func (NumberForm) Ge

func (r NumberForm) Ge(n any) (is bool)

Ge returns a boolean value indicative of whether the receiver is greater than or equal to the value provided.

This method is merely a convenient wrapper to an ORed call of the NumberForm.Gt and NumberForm.Equal methods.

Valid input types are string, uint64, int, uint, *math/big.Int and NumberForm.

Any input that represents a negative or unspecified number guarantees a false return.

Example
nf, _ := NewNumberForm(4658)
oth, _ := NewNumberForm(4501)
fmt.Printf("%s >= %s: %t", nf, oth, nf.Ge(oth))
Output:

4658 >= 4501: true

func (NumberForm) Gt

func (r NumberForm) Gt(n any) (is bool)

Gt returns a boolean value indicative of whether the receiver is greater than the value provided.

Valid input types are string, uint64, int, uint, *math/big.Int and NumberForm.

Any input that represents a negative or unspecified number guarantees a false return.

Example
nf, _ := NewNumberForm(4658)
oth := `4501`
fmt.Printf("%s > %s: %t", nf, oth, nf.Gt(oth))
Output:

4658 > 4501: true
Example (ByString)
nf, _ := NewNumberForm(`4658`)
oth := `4501`
fmt.Printf("%s > %s: %t", nf, oth, nf.Gt(oth))
Output:

4658 > 4501: true
Example (ByUint64)
nf, _ := NewNumberForm(uint64(4658))
oth := uint64(4501)
fmt.Printf("%s > %d: %t", nf, oth, nf.Gt(oth))
Output:

4658 > 4501: true

func (*NumberForm) IsZero

func (r *NumberForm) IsZero() (is bool)

IsZero returns a Boolean value indicative of whether the receiver instance is nil, or unset.

Example
var nf NumberForm
fmt.Printf("Zero: %t", nf.IsZero())
Output:

Zero: true

func (NumberForm) Le

func (r NumberForm) Le(n any) (is bool)

Le returns a boolean value indicative of whether the receiver is less than or equal to the value provided.

This method is merely a convenient wrapper to an ORed call of the NumberForm.Lt and NumberForm.Equal methods.

Valid input types are string, uint64, int, uint, *math/big.Int and NumberForm.

Any input that represents a negative or unspecified number guarantees a false return.

Example
nf, _ := NewNumberForm(4658)
oth, _ := NewNumberForm(4501)
fmt.Printf("%s =< %s: %t", nf, oth, nf.Le(oth))
Output:

4658 =< 4501: false

func (NumberForm) Lt

func (r NumberForm) Lt(n any) (is bool)

Lt returns a boolean value indicative of whether the receiver is less than the value provided.

Valid input types are string, uint64, int, uint, *math/big.Int and NumberForm.

Any input that represents a negative or unspecified number guarantees a false return.

Example
nf, _ := NewNumberForm(4658)
oth, _ := NewNumberForm(4501)
fmt.Printf("%s < %s: %t", nf, oth, nf.Lt(oth))
Output:

4658 < 4501: false
Example (ByString)
nf, _ := NewNumberForm(`4658`)
oth := `4501`
fmt.Printf("%s < %s: %t", nf, oth, nf.Lt(oth))
Output:

4658 < 4501: false
Example (ByUint64)
nf, _ := NewNumberForm(uint64(4658))
oth := uint64(4501)
fmt.Printf("%s < %d: %t", nf, oth, nf.Lt(oth))
Output:

4658 < 4501: false

func (NumberForm) String

func (r NumberForm) String() string

String returns the base-10 string representation of the receiver instance.

Example
nf, _ := NewNumberForm(4658)
fmt.Printf("%s", nf)
Output:

4658

func (NumberForm) Valid

func (r NumberForm) Valid() bool

Valid returns a Boolean value indicative of proper initialization.

Example
nf, _ := NewNumberForm(4658)
fmt.Printf("Valid: %t", nf.Valid())
Output:

Valid: true

type OID

type OID struct {
	// contains filtered or unexported fields
}

OID contains an underlying ASN1Notation value, and extends convenient methods allowing interrogation and verification.

func NewOID

func NewOID(x any) (r *OID, err error)

NewOID creates an instance of OID and returns it alongside an error.

Valid input forms for ASN.1 values are:

Not all NameAndNumberForm values (arcs) require actual names; they can be numbers alone or in the so-called nameAndNumber syntax (name(Number)). For example:

{iso(1) identified-organization(3) 6}

... is perfectly valid, but generally NOT recommended when clarity or precision is desired.

Note that the following root node abbreviations are supported:

  • `itu-t` resolves to itu-t(0)
  • `iso` resolves to iso(1)
  • `joint-iso-itu-t` resolves to joint-iso-itu-t(2)

Case is significant during processing of the above abbreviations. Note that it is inappropriate to utilize these abbreviations for any portion of an OID instance other than as the respective root node.

NumberForm values CANNOT be negative, but are unbounded in their magnitude.

Example
// X.667 example
a := `{joint-iso-itu-t(2) uuid(25) ans(987895962269883002155146617097157934)}`
id, err := NewOID(a)
if err != nil {
	fmt.Println(err)
	return
}
fmt.Printf("ASN.1 Notation: %s", id.ASN())
Output:

ASN.1 Notation: {joint-iso-itu-t(2) uuid(25) ans(987895962269883002155146617097157934)}

func (OID) ASN

func (r OID) ASN() (a ASN1Notation)

ASN returns the underlying ASN1Notation instance found within the receiver.

Example
raw := `{iso(1) identified-organization(3) dod(6) internet(1) private(4) enterprise(1) 56521 example(999)}`
id, err := NewOID(raw)
if err != nil {
	fmt.Println(err)
	return
}

fmt.Printf("%s", id.ASN())
Output:

{iso(1) identified-organization(3) dod(6) internet(1) private(4) enterprise(1) 56521 example(999)}

func (OID) Dot

func (r OID) Dot() (d DotNotation)

Dot returns a DotNotation instance based on the contents of the underlying ASN1Notation instance found within the receiver.

Note that at a receiver length of two (2) or more is required for successful output.

Example
raw := `{iso(1) identified-organization(3) dod(6) internet(1) private(4) enterprise(1) 56521 example(999)}`
id, err := NewOID(raw)
if err != nil {
	fmt.Println(err)
	return
}
fmt.Printf("%s", id.Dot())
Output:

1.3.6.1.4.1.56521.999
Example (Bogus)

This example demonstrates a bogus DotNotation output due to the presence of less than two (2) NameAndNumberForm instances within the receiver.

DotNotation ALWAYS requires two (2) or more elements to be considered valid.

id, err := NewOID(`{iso(1)}`)
if err != nil {
	fmt.Println(err)
	return
}
dot := id.Dot()
fmt.Println(dot)
Output:

func (*OID) IsZero

func (r *OID) IsZero() (is bool)

IsZero checks the receiver for nilness and returns a Boolean indicative of the result.

Example
var z OID
fmt.Printf("Zero: %t", z.IsZero())
Output:

Zero: true

func (OID) Leaf

func (r OID) Leaf() (nanf NameAndNumberForm)

Leaf returns the leaf-node instance of NameAndNumberForm.

Example
a := `{joint-iso-itu-t(2) uuid(25) ans(987895962269883002155146617097157934)}`
id, err := NewOID(a)
if err != nil {
	fmt.Println(err)
	return
}
fmt.Printf("Leaf node: %s", id.Leaf())
Output:

Leaf node: ans(987895962269883002155146617097157934)

func (OID) Len

func (r OID) Len() (i int)

Len returns the integer length of all underlying NumberForm values present within the receiver.

Example
raw := `{iso(1) identified-organization(3) dod(6) internet(1) private(4) enterprise(1) 56521 example(999)}`
id, err := NewOID(raw)
if err != nil {
	fmt.Println(err)
	return
}
fmt.Printf("%d", id.Len())
Output:

8

func (OID) Parent

func (r OID) Parent() (nanf NameAndNumberForm)

Parent returns the leaf-node's Parent instance of NameAndNumberForm.

Example
a := `{joint-iso-itu-t(2) uuid(25) ans(987895962269883002155146617097157934)}`
id, err := NewOID(a)
if err != nil {
	fmt.Println(err)
	return
}
fmt.Printf("Leaf node parent: %s", id.Parent())
Output:

Leaf node parent: uuid(25)

func (OID) Root

func (r OID) Root() (nanf NameAndNumberForm)

Root returns the root node instance of NameAndNumberForm.

Example
a := `{joint-iso-itu-t(2) uuid(25) ans(987895962269883002155146617097157934)}`
id, err := NewOID(a)
if err != nil {
	fmt.Println(err)
	return
}
fmt.Printf("Root node: %s", id.Root())
Output:

Root node: joint-iso-itu-t(2)

func (OID) Valid

func (r OID) Valid() (ok bool)

Valid returns a Boolean value indicative of whether the receiver's state is considered value.

Example
var o OID
fmt.Printf("Valid: %t", o.Valid())
Output:

Valid: false

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL