2020-04-03 23:11:33 +00:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"regexp"
|
2021-07-22 03:52:37 +00:00
|
|
|
"strings"
|
2020-04-03 23:11:33 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2021-05-04 22:06:05 +00:00
|
|
|
const (
|
|
|
|
// RFC3339Zero is the default value for time.Time.Unix().
|
|
|
|
RFC3339Zero = int64(-62135596800)
|
2020-04-03 23:11:33 +00:00
|
|
|
|
2021-08-05 04:17:07 +00:00
|
|
|
clean = "clean"
|
|
|
|
tagged = "tagged"
|
|
|
|
unknown = "unknown"
|
|
|
|
)
|
|
|
|
|
2022-08-07 11:13:56 +00:00
|
|
|
const (
|
|
|
|
period = "."
|
|
|
|
https = "https"
|
2022-09-03 01:51:02 +00:00
|
|
|
wss = "wss"
|
2022-08-07 11:13:56 +00:00
|
|
|
)
|
|
|
|
|
2022-06-27 08:27:57 +00:00
|
|
|
// X.509 consts.
|
|
|
|
const (
|
|
|
|
BlockTypeRSAPrivateKey = "RSA PRIVATE KEY"
|
|
|
|
BlockTypeRSAPublicKey = "RSA PUBLIC KEY"
|
|
|
|
BlockTypeECDSAPrivateKey = "EC PRIVATE KEY"
|
|
|
|
BlockTypePKCS8PrivateKey = "PRIVATE KEY"
|
|
|
|
BlockTypePKIXPublicKey = "PUBLIC KEY"
|
|
|
|
BlockTypeCertificate = "CERTIFICATE"
|
|
|
|
BlockTypeCertificateRequest = "CERTIFICATE REQUEST"
|
|
|
|
|
|
|
|
KeyAlgorithmRSA = "RSA"
|
|
|
|
KeyAlgorithmECDSA = "ECDSA"
|
|
|
|
KeyAlgorithmEd25519 = "ED25519"
|
|
|
|
|
|
|
|
HashAlgorithmSHA1 = "SHA1"
|
|
|
|
HashAlgorithmSHA256 = "SHA256"
|
|
|
|
HashAlgorithmSHA384 = "SHA384"
|
|
|
|
HashAlgorithmSHA512 = "SHA512"
|
|
|
|
|
|
|
|
EllipticCurveP224 = "P224"
|
|
|
|
EllipticCurveP256 = "P256"
|
|
|
|
EllipticCurveP384 = "P384"
|
|
|
|
EllipticCurveP521 = "P521"
|
|
|
|
|
|
|
|
EllipticCurveAltP224 = "P-224"
|
|
|
|
EllipticCurveAltP256 = "P-256"
|
|
|
|
EllipticCurveAltP384 = "P-384"
|
|
|
|
EllipticCurveAltP521 = "P-521"
|
|
|
|
)
|
|
|
|
|
2021-08-05 04:17:07 +00:00
|
|
|
const (
|
2021-05-04 22:06:05 +00:00
|
|
|
// Hour is an int based representation of the time unit.
|
2022-07-22 21:59:14 +00:00
|
|
|
Hour = time.Minute * 60
|
2020-05-02 16:20:40 +00:00
|
|
|
|
2021-05-04 22:06:05 +00:00
|
|
|
// Day is an int based representation of the time unit.
|
|
|
|
Day = Hour * 24
|
2021-03-22 09:04:09 +00:00
|
|
|
|
2021-05-04 22:06:05 +00:00
|
|
|
// Week is an int based representation of the time unit.
|
|
|
|
Week = Day * 7
|
|
|
|
|
|
|
|
// Year is an int based representation of the time unit.
|
|
|
|
Year = Day * 365
|
|
|
|
|
|
|
|
// Month is an int based representation of the time unit.
|
|
|
|
Month = Year / 12
|
2021-08-05 04:17:07 +00:00
|
|
|
)
|
2021-06-18 04:35:43 +00:00
|
|
|
|
2021-08-02 11:55:30 +00:00
|
|
|
var (
|
2022-03-02 06:40:26 +00:00
|
|
|
standardDurationUnits = []string{"ns", "us", "µs", "μs", "ms", "s", "m", "h"}
|
|
|
|
reDurationSeconds = regexp.MustCompile(`^\d+$`)
|
|
|
|
reDurationStandard = regexp.MustCompile(`(?P<Duration>[1-9]\d*?)(?P<Unit>[^\d\s]+)`)
|
|
|
|
)
|
|
|
|
|
|
|
|
// Duration unit types.
|
|
|
|
const (
|
|
|
|
DurationUnitDays = "d"
|
|
|
|
DurationUnitWeeks = "w"
|
|
|
|
DurationUnitMonths = "M"
|
|
|
|
DurationUnitYears = "y"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Number of hours in particular measurements of time.
|
|
|
|
const (
|
|
|
|
HoursInDay = 24
|
|
|
|
HoursInWeek = HoursInDay * 7
|
|
|
|
HoursInMonth = HoursInDay * 30
|
|
|
|
HoursInYear = HoursInDay * 365
|
2021-08-02 11:55:30 +00:00
|
|
|
)
|
2020-08-21 02:16:23 +00:00
|
|
|
|
2022-12-21 10:31:21 +00:00
|
|
|
const (
|
|
|
|
// timeUnixEpochAsMicrosoftNTEpoch represents the unix epoch as a Microsoft NT Epoch.
|
|
|
|
// The Microsoft NT Epoch is ticks since Jan 1, 1601 (1 tick is 100ns).
|
|
|
|
timeUnixEpochAsMicrosoftNTEpoch uint64 = 116444736000000000
|
|
|
|
)
|
|
|
|
|
2022-10-17 10:51:59 +00:00
|
|
|
const (
|
2022-11-04 00:32:49 +00:00
|
|
|
// CharSetAlphabeticLower are literally just valid alphabetic lowercase printable ASCII chars.
|
|
|
|
CharSetAlphabeticLower = "abcdefghijklmnopqrstuvwxyz"
|
|
|
|
|
|
|
|
// CharSetAlphabeticUpper are literally just valid alphabetic uppercase printable ASCII chars.
|
|
|
|
CharSetAlphabeticUpper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
|
|
|
2022-10-17 10:51:59 +00:00
|
|
|
// CharSetAlphabetic are literally just valid alphabetic printable ASCII chars.
|
2022-11-04 00:32:49 +00:00
|
|
|
CharSetAlphabetic = CharSetAlphabeticLower + CharSetAlphabeticUpper
|
2022-10-17 10:51:59 +00:00
|
|
|
|
|
|
|
// CharSetNumeric are literally just valid numeric chars.
|
|
|
|
CharSetNumeric = "0123456789"
|
|
|
|
|
|
|
|
// CharSetNumericHex are literally just valid hexadecimal printable ASCII chars.
|
|
|
|
CharSetNumericHex = CharSetNumeric + "ABCDEF"
|
|
|
|
|
|
|
|
// CharSetSymbolic are literally just valid symbolic printable ASCII chars.
|
|
|
|
CharSetSymbolic = "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"
|
|
|
|
|
2022-11-04 00:32:49 +00:00
|
|
|
// CharSetSymbolicRFC3986Unreserved are RFC3986 unreserved symbol characters.
|
|
|
|
// See https://www.rfc-editor.org/rfc/rfc3986#section-2.3.
|
|
|
|
CharSetSymbolicRFC3986Unreserved = "-._~"
|
|
|
|
|
2022-10-17 10:51:59 +00:00
|
|
|
// CharSetAlphaNumeric are literally just valid alphanumeric printable ASCII chars.
|
|
|
|
CharSetAlphaNumeric = CharSetAlphabetic + CharSetNumeric
|
|
|
|
|
|
|
|
// CharSetASCII are literally just valid printable ASCII chars.
|
|
|
|
CharSetASCII = CharSetAlphabetic + CharSetNumeric + CharSetSymbolic
|
2022-11-04 00:32:49 +00:00
|
|
|
|
|
|
|
// CharSetRFC3986Unreserved are RFC3986 unreserved characters.
|
|
|
|
// See https://www.rfc-editor.org/rfc/rfc3986#section-2.3.
|
2022-11-04 11:24:10 +00:00
|
|
|
CharSetRFC3986Unreserved = CharSetAlphabetic + CharSetNumeric + CharSetSymbolicRFC3986Unreserved
|
2021-11-11 09:13:32 +00:00
|
|
|
)
|
2020-12-03 05:23:52 +00:00
|
|
|
|
2021-07-22 03:52:37 +00:00
|
|
|
var htmlEscaper = strings.NewReplacer(
|
|
|
|
"&", "&",
|
|
|
|
"<", "<",
|
|
|
|
">", ">",
|
|
|
|
`"`, """,
|
|
|
|
"'", "'",
|
|
|
|
)
|
2021-08-02 11:55:30 +00:00
|
|
|
|
|
|
|
// ErrTimeoutReached error thrown when a timeout is reached.
|
|
|
|
var ErrTimeoutReached = errors.New("timeout reached")
|
|
|
|
|
2022-12-21 10:31:21 +00:00
|
|
|
const (
|
|
|
|
windows = "windows"
|
|
|
|
errFmtLinuxNotFound = "open %s: no such file or directory"
|
|
|
|
)
|