utils

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2025 License: GPL-3.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ComputeHash

func ComputeHash(data []byte) []byte

ComputeHash calculates the SHA-256 hash of the provided data.

Parameters: - data: A byte slice containing the input data to be hashed.

Returns: - A byte slice containing the SHA-256 hash of the input data.

Note: This function uses the SHA-256 hashing algorithm from the crypto/sha256 package to generate a fixed-size 32-byte hash. The resulting hash can be used for various purposes, such as data integrity verification and cryptographic operations.

func ComputeKeyIdentifier

func ComputeKeyIdentifier(keyMaterial []byte, version version.KeyCredentialVersion) string

ComputeKeyIdentifier generates a key identifier based on the provided key material and version.

Parameters: - keyMaterial: A byte slice containing the key material to be used for generating the key identifier. - version: A version.KeyCredentialVersion value representing the version of the key credential.

Returns: - A string representing the generated key identifier.

Note: This function first computes the SHA-256 hash of the provided key material using the ComputeHash function. It then converts the resulting binary hash to a string representation based on the specified version using the ConvertFromBinaryIdentifier function. The generated key identifier can be used for various purposes, such as uniquely identifying cryptographic keys and credentials.

func ConvertFromBinaryIdentifier

func ConvertFromBinaryIdentifier(keyIdentifier []byte, kcv version.KeyCredentialVersion) string

ConvertFromBinaryIdentifier converts a binary key identifier to its string representation.

Parameters: - keyIdentifier: A byte slice containing the binary representation of the key identifier. - version: A KeyCredentialVersion object representing the version of the key credential.

Returns: - A string representing the key identifier.

Note: The function handles different versions of key credentials as follows: - For version 0 and 1, the key identifier is encoded to a hexadecimal string using hex.EncodeToString. - For version 2, the key identifier is encoded to a base64 string using base64.StdEncoding.EncodeToString. - For any other version, the key identifier is treated as base64 format and is encoded using base64.StdEncoding.EncodeToString.

func ConvertToBinaryIdentifier

func ConvertToBinaryIdentifier(keyIdentifier string, kcv version.KeyCredentialVersion) ([]byte, error)

ConvertToBinaryIdentifier converts a key identifier string to its binary representation.

Parameters:

- keyIdentifier: A string representing the key identifier to be converted.

- version: A KeyCredentialVersion object representing the version of the key credential.

Returns:

- A byte slice containing the binary representation of the key identifier.

- An error if the conversion fails.

Note:

The function handles different versions of key credentials as follows:

- For version 0 and 1, the key identifier is expected to be in hexadecimal format and is decoded using hex.DecodeString.

- For version 2, the key identifier is expected to be in base64 format and is decoded using base64.StdEncoding.DecodeString with padding.

- For any other version, the key identifier is treated as base64 format and is decoded using base64.StdEncoding.DecodeString with padding.

func ConvertToBinaryTime

func ConvertToBinaryTime(date time.Time, ksrc source.KeySource, kcv version.KeyCredentialVersion) []byte

ConvertToBinaryTime converts a time.Time object to its binary representation in little-endian format.

Parameters: - date: A time.Time object representing the time to be converted. - source: The source of the key, which can affect the interpretation of the time. - version: The version of the KeyCredential, which can affect the interpretation of the time.

Returns: - A byte slice containing the binary representation of the time in little-endian format.

Note: The function currently treats all versions and sources the same way, converting the time directly to a Unix time in nanoseconds and then encoding it in little-endian format.

Types

type DateTime

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

DateTime represents a point in time with additional precision.

Fields: - Time: A time.Time object representing the point in time. - Ticks: A uint64 value representing the number of 100-nanosecond intervals that have elapsed since 1601-01-01 00:00:00 UTC.

Methods: - NewDateTime: Initializes a new DateTime instance based on the provided ticks or the current time if ticks is 0. - ToUniversalTime: Converts the DateTime instance to a time.Time object in UTC.

Note: The Ticks field provides additional precision for representing time, which is useful for certain applications that require high-resolution timestamps. The Time field is a standard time.Time object that can be used with Go's time package functions.

func ConvertFromBinaryTime

func ConvertFromBinaryTime(rawBinaryTime []byte, ksrc source.KeySource, kcv version.KeyCredentialVersion) DateTime

ConvertFromBinaryTime converts a binary representation of time to a time.Time object. The binary representation is expected to be in little-endian format.

Parameters: - rawBinaryTime: A byte slice containing the binary representation of the time. - source: The source of the key, which can affect the interpretation of the time. - version: The version of the KeyCredential, which can affect the interpretation of the time.

Returns: - A time.Time object representing the converted time.

Note: The function currently treats all versions and sources the same way, converting the binary timestamp directly to a Unix time in nanoseconds.

Src : https://github.com/microsoft/referencesource/blob/master/mscorlib/system/datetime.cs

func NewDateTime

func NewDateTime(ticks uint64) DateTime

NewDateTime initializes a new DateTime instance.

Parameters:

  • ticks: A uint64 value representing the number of 100-nanosecond intervals that have elapsed since 1601-01-01 00:00:00 UTC. If ticks is 0, the function sets the current time and calculates ticks from 1601 to now.

Returns: - A DateTime object initialized with the provided ticks or the current time if ticks is 0.

Note: The function calculates the number of nanoseconds between 1601-01-01 and the UNIX epoch (1970-01-01) to convert ticks to a time.Time object. If ticks is 0, the function sets the current time and calculates the ticks from 1601 to the current time. Otherwise, it sets the time based on the provided ticks.

func (*DateTime) GetTicks added in v1.0.3

func (dt *DateTime) GetTicks() uint64

GetTicks returns the number of 100-nanosecond intervals (ticks) stored in the DateTime instance.

Returns: - A uint64 value representing the number of 100-nanosecond intervals since 1601-01-01 00:00:00 UTC.

func (*DateTime) GetTime added in v1.0.3

func (dt *DateTime) GetTime() time.Time

GetTime returns the time.Time value stored in the DateTime instance.

Returns: - A time.Time value representing the stored time.

func (DateTime) Marshal added in v1.0.3

func (dt DateTime) Marshal() ([]byte, error)

Marshal converts the DateTime instance to its binary representation in little-endian format.

Returns: - A byte slice containing the binary representation of the DateTime instance in little-endian format, otherwise an error.

Note: This function encodes the Ticks field of the DateTime instance as a 64-bit unsigned integer in little-endian format. The function returns a byte slice containing the binary representation of the DateTime instance in little-endian format.

func (*DateTime) SetTicks added in v1.0.3

func (dt *DateTime) SetTicks(ticks uint64)

SetTicks sets the number of 100-nanosecond intervals (ticks) for the DateTime instance.

Parameters: - ticks: A uint64 value representing the number of 100-nanosecond intervals since 1601-01-01 00:00:00 UTC.

Note: This function updates both the Ticks field and recalculates the corresponding Time field based on the provided ticks.

func (*DateTime) SetTime added in v1.0.3

func (dt *DateTime) SetTime(t time.Time)

SetTime sets the time for the DateTime instance.

Parameters: - t: A time.Time value to set.

Note: This function updates both the Time field and recalculates the corresponding Ticks field based on the provided time.

func (DateTime) String

func (dt DateTime) String() string

String returns the string representation of the DateTime instance.

Returns: - A string representing the DateTime instance in the default format used by the time.Time type.

Note: This function leverages the String method of the embedded time.Time type to provide a human-readable representation of the DateTime instance. The format typically includes the date, time, and time zone.

func (DateTime) ToTicks

func (dt DateTime) ToTicks() uint64

ToTicks returns the number of 100-nanosecond intervals (ticks) that have elapsed since 1601-01-01 00:00:00 UTC.

Returns: - A uint64 value representing the number of 100-nanosecond intervals (ticks) since 1601-01-01 00:00:00 UTC.

Note: This function provides a way to retrieve the internal tick count of the DateTime instance, which is useful for binary time representations and calculations.

func (DateTime) ToUniversalTime

func (dt DateTime) ToUniversalTime() time.Time

ToUniversalTime converts the DateTime instance to a time.Time object in UTC.

Returns: - A time.Time object representing the DateTime instance in Coordinated Universal Time (UTC).

Note: This function ensures that the time is represented in the UTC time zone, regardless of the original time zone of the DateTime instance.

func (*DateTime) Unmarshal added in v1.0.3

func (dt *DateTime) Unmarshal(data []byte) error

Unmarshal converts the binary representation of the DateTime instance to a DateTime object.

Parameters: - data: A byte slice containing the binary representation of the DateTime instance.

Returns: - An error if the unmarshalling fails, otherwise nil.

Note: This function decodes the Ticks field of the DateTime instance from a 64-bit unsigned integer in little-endian format. The function expects the data to be a 64-bit unsigned integer in little-endian format.

Jump to

Keyboard shortcuts

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