util

package
v0.0.0-...-29ce5d4 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2024 License: Apache-2.0 Imports: 29 Imported by: 12

Documentation

Overview

Package util implements a set of util functions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddressCount

func AddressCount(network *net.IPNet) uint64

AddressCount returns the number of distinct host addresses within the given CIDR range.

Since the result is a uint64, this function returns meaningful information only for IPv4 ranges and IPv6 ranges with a prefix size of at least 65.

func AddressRange

func AddressRange(network *net.IPNet) (net.IP, net.IP)

AddressRange returns the first and last addresses in the given CIDR range.

func AesECBEncryptHex

func AesECBEncryptHex(key, message string) (string, error)

AesECBEncryptHex returns an AES encrypted string

func Base64Encode

func Base64Encode(data []byte) string

Base64Encode gets base64 encoded string from data.

func CheckFileExists

func CheckFileExists(filename string) bool

CheckFileExists checks if specified file exists.

func Contains

func Contains(slice []string, value string, caseInsensitive bool) bool

Contains determines whether a string slice contains a certain value. Ignore case when comparing if case insensitive.

func CreateRandomString

func CreateRandomString() string

CreateRandomString create random string

func Debug

func Debug(title, message string)

Debug generates debug info for debug mode.

func Dec

func Dec(IP net.IP) net.IP

Dec decreases the IP by one this returns a new []byte for the IP

func FormatTest

func FormatTest(funcName, got, expected string) string

FormatTest returns a formatted string for unit test.

func GetMD5

func GetMD5(data interface{}, base64Encode bool) string

GetMD5 gets the MD5 value from data. Param base64Encode determines whether use Base64Encode meanwhile.

func GetMapKey

func GetMapKey(m map[string]string, key string, caseInsensitive bool) string

GetMapKey returns the key of the map for a certain value. Ignore case when comparing if case insensitive.

func GetMapValue

func GetMapValue(m map[string]string, key string, caseInsensitive bool) string

GetMapValue returns the value of the map for a certain key. Ignore case when comparing if case insensitive.

func GetSha256

func GetSha256(data interface{}) string

GetSha256 gets SHA256 value from data.

func GetURIPath

func GetURIPath(uri string) string

GetURIPath returns the path part of URI.

func GetURL

func GetURL(protocol, host, uriPath string, params map[string]string) string

GetURL gets the full URL for a http request.

func GuessMimeType

func GuessMimeType(s string) string

GuessMimeType gets the MIME Type of a suffix string.

func HmacSha256Hex

func HmacSha256Hex(key, message string) string

HmacSha256Hex returns a encrypted string.

func HomeDir

func HomeDir() (string, error)

HomeDir returns the HOME directory of current login user.

func Host

func Host(base *net.IPNet, num int) (net.IP, error)

Host takes a parent CIDR range and turns it into a host IP address with the given host number.

For example, 10.3.0.0/16 with a host number of 2 gives 10.3.0.2.

func HostToURL

func HostToURL(host, protocol string) string

HostToURL returns the whole URL string.

func Inc

func Inc(IP net.IP) net.IP

Inc increases the IP by one this returns a new []byte for the IP

func MapContains

func MapContains(m map[string]string, compareFunc func(string, string) bool) bool

MapContains determines whether the string map contains a uncertain value. The result is determined by compare function.

func MapKeyToLower

func MapKeyToLower(m map[string]string)

MapKeyToLower transforms each item of a map to lowercase.

func NewECBEncrypter

func NewECBEncrypter(b cipher.Block) cipher.BlockMode

NewECBEncrypter returns a ECBEncrypter

func NextSubnet

func NextSubnet(network *net.IPNet, prefixLen int) (*net.IPNet, bool)

NextSubnet returns the next available subnet of the desired mask size starting for the maximum IP of the offset subnet If the IP exceeds the maxium IP then the second return value is true

func PKCS7Padding

func PKCS7Padding(ciphertext []byte, blockSize int) []byte

PKCS7Padding returns a encrypted string.

func PreviousSubnet

func PreviousSubnet(network *net.IPNet, prefixLen int) (*net.IPNet, bool)

PreviousSubnet returns the subnet of the desired mask in the IP space just lower than the start of IPNet provided. If the IP space rolls over then the second return value is true

func SliceToLower

func SliceToLower(slice []string)

SliceToLower transforms each item of a slice to lowercase.

func Subnet

func Subnet(base *net.IPNet, newBits int, num int) (*net.IPNet, error)

Subnet takes a parent CIDR range and creates a subnet within it with the given number of additional prefix bits and the given network number.

For example, 10.3.0.0/16, extended by 8 bits, with a network number of 5, becomes 10.3.5.0/24 .

func TempFile

func TempFile(content []byte, dir, prefix string) (*os.File, error)

TempFile generates a temp file with separated content.

func TempFileWithSize

func TempFileWithSize(fileSize int64) (*os.File, error)

TempFileWithSize generates a temp file with specified size.

func TimeStringToRFC1123

func TimeStringToRFC1123(str string) string

TimeStringToRFC1123 returns a formatted string of `time.RFC1123` format.

func TimeToUTCString

func TimeToUTCString(t time.Time) string

TimeToUTCString returns a utc string of a time instance.

func ToCanonicalHeaderString

func ToCanonicalHeaderString(headerMap map[string]string) string

ToCanonicalHeaderString returns the canonicalized string.

func ToCanonicalQueryString

func ToCanonicalQueryString(params map[string]string) string

ToCanonicalQueryString returns the canonicalized query string.

func ToJson

func ToJson(i interface{}, keys ...string) ([]byte, error)

ToJson converts anything to JSON.

func ToMap

func ToMap(i interface{}, keys ...string) (map[string]interface{}, error)

ToMap converts anything to a map instance.

func URIEncodeExceptSlash

func URIEncodeExceptSlash(uri string) string

URIEncodeExceptSlash encodes all characters of a string except the slash character.

func URLEncode

func URLEncode(str string) string

URLEncode encodes a string like Javascript's encodeURIComponent()

func VerifyNoOverlap

func VerifyNoOverlap(subnets []*net.IPNet, CIDRBlock *net.IPNet) error

VerifyNoOverlap takes a list subnets and supernet (CIDRBlock) and verifies none of the subnets overlap and all subnets are in the supernet it returns an error if any of those conditions are not satisfied

Types

This section is empty.

Notes

Bugs

Jump to

Keyboard shortcuts

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