rnd

package
v0.0.0-...-c8acfb9 Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2024 License: AGPL-3.0 Imports: 16 Imported by: 0

Documentation

Overview

Package rnd provides random token generation and validation.

Copyright (c) 2018 - 2024 PhotoPrism UG. All rights reserved.

This program is free software: you can redistribute it and/or modify
it under Version 3 of the GNU Affero General Public License (the "AGPL"):
<https://docs.photoprism.app/license/agpl>

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

The AGPL is supplemented by our Trademark and Brand Guidelines,
which describe how our Brand Assets may be used:
<https://www.photoprism.app/trademark>

Feel free to send an email to hello@photoprism.app if you have questions, want to support our work, or just want to say hello.

Additional information can be found in our Developer Guide: <https://docs.photoprism.app/developer-guide/>

Index

Constants

View Source
const (
	SessionIdLength      = 64
	AuthTokenLength      = 48
	AppPasswordLength    = 27
	AppPasswordSeparator = '-'
)
View Source
const (
	PrefixNone  = byte(0)
	PrefixMixed = byte('*')
)
View Source
const CharsetBase36 = checksum.CharsetBase36
View Source
const CharsetBase62 = checksum.CharsetBase62
View Source
const (
	ClientSecretLength = 32
)

Variables

This section is empty.

Functions

func AppPassword

func AppPassword() string

AppPassword generates a random, human-friendly authentication token that can also be used as password replacement for client applications. It is separated by 3 dashes for better readability and has a total length of 27 characters.

Example: OXiV72-wTtiL9-d04jO7-X7XP4p

func AuthKey

func AuthKey(issuer, accountName string) (key *otp.Key, err error)

AuthKey returns a randomly initialized, time-based one-time password key, e.g. for 2-factor authentication.

func AuthToken

func AuthToken() string

AuthToken generates a random hexadecimal character token for authenticating client applications.

Examples: 9fa8e562564dac91b96881040e98f6719212a1a364e0bb25

func Base36

func Base36(length int) string

Base36 generates a random token containing lowercase letters and numbers.

func Base62

func Base62(length int) string

Base62 generates a random token containing upper and lower case letters as well as numbers.

func Charset

func Charset(length int, charset string) string

Charset generates a random token with the specified length and charset.

func ClientSecret

func ClientSecret() string

ClientSecret generates a random client secret containing 32 upper and lower case letters as well as numbers.

func ContainsUID

func ContainsUID(s []string, prefix byte) bool

ContainsUID checks if a slice of strings contains ContainsUID only.

func CrcToken

func CrcToken() string

CrcToken returns a string token with checksum.

func GenerateUID

func GenerateUID(prefix byte) string

GenerateUID returns a unique id with prefix as string.

func InvalidRefID

func InvalidRefID(s string) bool

InvalidRefID checks if the reference ID is empty or invalid.

func InvalidUID

func InvalidUID(s string, prefix byte) bool

InvalidUID checks if the UID is empty or invalid.

func IsAlnum

func IsAlnum(s string) bool

IsAlnum returns true if the string only contains alphanumeric ascii chars without whitespace.

func IsAppPassword

func IsAppPassword(s string, verifyChecksum bool) bool

IsAppPassword checks if the string represents a valid app password.

func IsAuthAny

func IsAuthAny(s string) bool

IsAuthAny checks if the string represents a valid auth token or app password.

func IsAuthToken

func IsAuthToken(s string) bool

IsAuthToken checks if the string represents a valid auth token.

func IsClientSecret

func IsClientSecret(s string) bool

IsClientSecret checks if the string represents a valid client secret.

func IsHex

func IsHex(s string) bool

IsHex returns true if the string only contains hex numbers, dashes and letters without whitespace.

func IsMD5

func IsMD5(s string) bool

IsMD5 checks if the string appears to be an MD5 hash. Example: 79054025255fb1a26e4bc422aef54eb4

func IsRefID

func IsRefID(s string) bool

IsRefID checks if the string is a valid reference ID.

func IsSHA1

func IsSHA1(s string) bool

IsSHA1 checks if the string appears to be a SHA1 hash. Example: de9f2c7fd25e1b3afad3e85a0bd17d9b100db4b3

func IsSHA224

func IsSHA224(s string) bool

IsSHA224 checks if the string appears to be a SHA224 hash. Example: d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f

func IsSHA256

func IsSHA256(s string) bool

IsSHA256 checks if the string appears to be a SHA256 hash. Example: e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855

func IsSHA384

func IsSHA384(s string) bool

IsSHA384 checks if the string appears to be a SHA384 hash. Example: 38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b

func IsSHA512

func IsSHA512(s string) bool

IsSHA512 checks if the string appears to be a SHA512 hash. Example: cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e

func IsSessionID

func IsSessionID(id string) bool

IsSessionID checks if the string represents a valid session id.

func IsUID

func IsUID(s string, prefix byte) bool

IsUID checks if the string is a valid entity UID.

func IsUUID

func IsUUID(s string) bool

IsUUID tests if the string looks like a standard UUID.

func IsUnique

func IsUnique(s string, prefix byte) bool

IsUnique checks if the string is a valid unique ID.

func Name

func Name() string

Name returns a pronounceable name consisting of a pet name and an adverb or adjective.

func NameN

func NameN(n int) string

NameN returns a pronounceable name consisting of a random combination of adverbs, an adjective, and a pet name.

func RandomBytes

func RandomBytes(n int) ([]byte, error)

RandomBytes returns cryptographically secure random bytes.

func RecoveryCode

func RecoveryCode() (code string)

RecoveryCode returns a randomly created recovery code for e.g. for 2-factor authentication.

func RefID

func RefID(id string) string

RefID generates a new reference ID with optional 2-character prefix.

func SanitizeUUID

func SanitizeUUID(s string) string

SanitizeUUID normalizes UUIDs found in XMP or Exif metadata.

func SessionID

func SessionID(token string) string

SessionID returns the hashed session id string.

func Sha224

func Sha224(b []byte) string

Sha224 returns the SHA224 checksum of the byte slice as a hex string.

func Sha256

func Sha256(b []byte) string

Sha256 returns the SHA256 checksum of the byte slice as a hex string.

func Sha512

func Sha512(b []byte) string

Sha512 returns the SHA512 checksum of the byte slice as a hex string.

func UUID

func UUID() string

UUID returns a random string based on RFC 4122 (UUID Version 4) or panics.

The strength of the UUID depends on the "crypto/rand" package.

func ValidateCrcToken

func ValidateCrcToken(s string) bool

ValidateCrcToken tests if the token string is valid.

Types

type Type

type Type string

Type represents a random id type.

const (
	TypeEmpty     Type = "empty"
	TypeMixed     Type = "mixed"
	TypeUUID      Type = "UUID"
	TypeUID       Type = "UID"
	TypeRefID     Type = "RID"
	TypeSessionID Type = "SID"
	TypeCrcToken  Type = "CRC"
	TypeMD5       Type = "MD5"
	TypeSHA1      Type = "SHA1"
	TypeSHA224    Type = "SHA224"
	TypeSHA256    Type = "SHA256"
	TypeSHA384    Type = "SHA384"
	TypeSHA512    Type = "SHA512"
	TypeUnknown   Type = "unknown"
)

func ContainsType

func ContainsType(ids []string) (idType Type, idPrefix byte)

ContainsType checks if a slice of strings contains only random IDs of a given type and returns it.

func IdType

func IdType(id string) (Type, byte)

IdType checks what kind of random ID a string contains and returns it along with the id prefix, if any.

func (Type) CrcToken

func (t Type) CrcToken() bool

func (Type) EntityID

func (t Type) EntityID() bool

func (Type) Equal

func (t Type) Equal(s string) bool

Equal checks if the type matches.

func (Type) Hash

func (t Type) Hash() bool

func (Type) NotEqual

func (t Type) NotEqual(s string) bool

NotEqual checks if the type is different.

func (Type) SHA

func (t Type) SHA() bool

func (Type) SHA1

func (t Type) SHA1() bool

func (Type) SHA2

func (t Type) SHA2() bool

func (Type) SessionID

func (t Type) SessionID() bool

func (Type) String

func (t Type) String() string

String returns the type as string.

func (Type) Unknown

func (t Type) Unknown() bool

Unknown checks if the type is unknown.

Jump to

Keyboard shortcuts

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