go_libs

package module
v2.8.0 Latest Latest
Warning

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

Go to latest
Published: Mar 24, 2022 License: MIT Imports: 21 Imported by: 3

README

go_libs

Go elements often included. This is the successor of the previous debugErrorCE package.

Crypt

RSA Key Creation

Keys can be created with the functions:

func CreateRSAKeyPair() (*rsa.PrivateKey, *rsa.PublicKey, error)

This function returns a 4096-bit RSA keypair or an error. It contains syntactic sugar as the private key always containts the public key. The second functions writes the keys directly to a file.

func CreateRSAKeyPair2File(outfileName string) error

Keys are automatically stored as a file. By default, the keys are 4096 bit, RSA keys. If the specified filename if foo, then the private key is stored in a file foo and the public key is stored in a file foo.pub. For compatibility reasons, the calls do not try to change the permissions of a file, in particular not of the private key file.

The function returns an error, if either foo or foo.pub already exists or cannot be created or written to. Both keys are stored in PEM format.

The key creation is compatible with openssl. My usual shell functions from the public github.com project (advertisement) PublicConfigurations allows us to verify if the private and the public key belong together and hereby are also recognised by openssl.

$ tlsRsaPrvFingerprint key1
5d30617645c9cf77d285e4ffd511c407f3aefaa7b2c45ecce6ceba73ccddd27c

$ tlsRsaPubFingerprint key1.pub 
5d30617645c9cf77d285e4ffd511c407f3aefaa7b2c45ecce6ceba73ccddd27c

Documentation

Index

Constants

This section is empty.

Variables

View Source
var OutputWriter io.Writer = os.Stderr

OutputWriter defines the default output channel. It can be changed if required.

Functions

func ByteArray2File added in v2.8.0

func ByteArray2File(file *os.File, bytes []byte) error

ByteArray2File writes a byte array into a file. If required it does so in multiple steps. If all succeeds then nil is returned, otherwise an error.

func ByteArray2ReponseWriter added in v2.8.0

func ByteArray2ReponseWriter(file http.ResponseWriter, bytes []byte) error

ByteArray2ReponseWriter writes a byte array into a file. If required it does so in multiple steps. If all succeeds then nil is returned, otherwise an error.

func CaptureOutput

func CaptureOutput(f func()) (stderr string, stdout string)

CaptureOutput get a function as its argument. It executes the function and returns the output (stderr and stdout) created by this function. While capturing this output, this output is not written to default stdout or stderr.

func CondDebug

func CondDebug(msg ...string)

CondDebug outputs if debug is set without an added newline at the EOL.

func CondDebugSet

func CondDebugSet(val bool)

CondDebugSet allows us to turn debug on/off.

func CondDebugStatus

func CondDebugStatus() bool

CondDebugStatus allows to check if debug is turned on/off.

func CondDebugln

func CondDebugln(msg ...string)

CondDebugln is the implementation of a global debug function. If it was turned on using CondDebugSet(true), then the string is shown to stderr. Else, no output is created.

func CreateRSAKeyPair

func CreateRSAKeyPair() (*rsa.PrivateKey, *rsa.PublicKey, error)

CreateRSAKeyPair creates an RSA 4096-bit key-pair. This function makes only partly sense, as the private key always contains the public key.

func CreateRSAKeyPair2File added in v2.1.0

func CreateRSAKeyPair2File(outfileName string) error

CreateRSAKeyPair2File checks if the 2 required files do not exist and can be created sucessfully. Then, it transfers control to createKeyPairError2.

func CurrentFunctionName

func CurrentFunctionName() string

CurrentFunctionName returns the name of the current function being executed.

func Debug

func Debug(msg ...string)

Debug outputs a message without adding a newline at the EOL

func Debugln

func Debugln(msg ...string)

Debugln outputs a message with adding a newline at the EOL

func ErrorExit

func ErrorExit(errorCode uint8, msg ...string)

ErrorExit exits the application with the specified error code. The output is written to the assigned output writer, by default stderr.

func ExecCmd

func ExecCmd(cmd string, args ...string) error

ExecCmd is a helper to execute an external application. If the exit status of this command is non-zero, then an error is returned, else nil.

func ExitIfError

func ExitIfError(err error, exitcode uint8, msg string)

ExitIfError exists using ErrorExit if the supplied err is not nil. In such a case, the error message of err will be added to the message.

func FilenameWithoutSuffix added in v2.5.0

func FilenameWithoutSuffix(filename string) string

func LoadPrivateKey

func LoadPrivateKey(filename string) (*rsa.PrivateKey, error)

LoadPrivateKey load a PEM-encoded RSA private key from a file

func LoadPublicKey added in v2.2.0

func LoadPublicKey(filename string) (*rsa.PublicKey, error)

LoadPublicKey load a PEM-encoded RSA public key from a file

func LogErr

func LogErr(msg string)

LogErr creates a message preprended with ERROR to syslog and stderr, but tries to continue execution.

func LogInfo

func LogInfo(msg string)

LogInfo creates an info error message to syslog and STDERR.

func LogInit

func LogInit(tag string)

LogInit tries to initialise the logging service.

func LogStringInit

func LogStringInit(tag string)

LogStringInit does not use syslog (for dockerised environments. Instead, it writes all messages to stderr) This is suited for dockerised environments.

func LogWarn

func LogWarn(msg string)

LogWarn creates a syslog and STDERR message labeled with WARNING.

func Pem2RsaPrivateKey added in v2.6.0

func Pem2RsaPrivateKey(der []byte) (*rsa.PrivateKey, error)

Pem2RsaPrivateKey load a PEM-encoded RSA private key from a buffer. The function does not try to read multiple keys from the byte array. Only the first PEM block is processed.

func Pem2RsaPublicKey added in v2.6.0

func Pem2RsaPublicKey(der []byte) (*rsa.PublicKey, error)

Pem2RsaPublicKey load a PEM-encoded RSA public key from a buffer. The function does not try to read multiple keys from the byte array. Only the first PEM block is processed.

func Sha256bytes2bytes

func Sha256bytes2bytes(bytes []byte) []byte

Sha256bytes2bytes converts a byte sequence into a SHA-256-based digest of it. The output for this application is the same on the commadn line with: curl -q localhost:8888 | jq -c .Data | tr -d '\n' | shasum -a256 The added newline must be removed. Alternatively, gnu-sed can be used instad of tr: gsed -Ez 's/\n$//' The complete JSON return structure only consists of US-ASCII characters. So potential different escaping for special characters do not have to be considered.

func Sign115ByteArray added in v2.3.0

func Sign115ByteArray(key *rsa.PrivateKey, digest []byte) ([]byte, error)

Sign115ByteArray returns a signature for the given digest or returns an error

func Sign115ByteArray2Base64 added in v2.3.0

func Sign115ByteArray2Base64(key *rsa.PrivateKey, digest []byte) (string, error)

Sign115ByteArray2Base64 signs a byte array by calling SignByteArray but returns the signature as a base64-encoded string.

func SignPSSByteArray added in v2.3.0

func SignPSSByteArray(key *rsa.PrivateKey, digest []byte) ([]byte, error)

SignPSSByteArray returns a signature for the given digest or returns an error

func SignPSSByteArray2Base64 added in v2.3.0

func SignPSSByteArray2Base64(key *rsa.PrivateKey, digest []byte) (string, error)

SignPSSByteArray2Base64 returns the signature as a base64-encoded string.

func Verify115Base64String added in v2.3.0

func Verify115Base64String(key *rsa.PublicKey, b64 string, msg string) error

Verify115Base64String accepts a base64 encoded string as the signature. It decodes the signature and calls VerifyByteArray.

func Verify115ByteArray added in v2.3.0

func Verify115ByteArray(key *rsa.PublicKey, digest []byte, msg []byte) error

Verify115ByteArray verifies a digital signature (digest). If no error is returned, then the verification was successful. Furthermore, it recalculates the digest of the message. It should result in the same digest as the digitally signed one.

func VerifyPSSBase64String added in v2.3.0

func VerifyPSSBase64String(key *rsa.PublicKey, b64 string, msg string) error

VerifyPSSBase64String accepts a base64 encoded string as the signature. It decodes the signature and calls VerifyByteArray.

func VerifyPSSByteArray added in v2.3.0

func VerifyPSSByteArray(key *rsa.PublicKey, digest []byte, msg []byte) error

VerifyPSSByteArray verifies a digital signature (digest). If no error is returned, then the verification was successful. Furthermore, it recalculates the digest of the message. It should result in the same digest as the digitally signed one.

func WriteRsaPrivateKey added in v2.6.0

func WriteRsaPrivateKey(file *os.File, privKey *rsa.PrivateKey) error

WriteRsaPrivateKey converts the key to PEM format and writes them to a file.

func WriteRsaPublicKey added in v2.6.0

func WriteRsaPublicKey(file *os.File, pubKey *rsa.PublicKey) error

WriteRsaPublicKey converts the public key to PEM format and writes them to the file.

Types

This section is empty.

Jump to

Keyboard shortcuts

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