commit f6eb52373f8ab99d8e3d6b8f346afefe2e13b4d4 Author: Unbewohnte Date: Sun Nov 20 17:46:20 2022 +0300 Initial commit diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..056fe5b --- /dev/null +++ b/LICENSE @@ -0,0 +1,9 @@ +The MIT License (MIT) + +Copyright © 2022 Kasyanov Nikolay Alexeyevich (Unbewohnte) + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..98decdf --- /dev/null +++ b/README.md @@ -0,0 +1,14 @@ +# GOAUXILIB +## A collection of various helper functions; AKA auxiliary Go library without any external dependencies + +# Use +Either + +- `go get unbewohnte.su:3000/Unbewohnte/goauxilib` + +or + +- Simply copy file(s) with needed functionality to your project + +# License +MIT \ No newline at end of file diff --git a/crypto.go b/crypto.go new file mode 100644 index 0000000..6db8609 --- /dev/null +++ b/crypto.go @@ -0,0 +1,81 @@ +/* +The MIT License (MIT) + +Copyright © 2022 Kasyanov Nikolay Alexeyevich (Unbewohnte) + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +*/ + +package goauxilib + +import ( + "crypto/aes" + "crypto/cipher" + "encoding/base64" +) + +// Encodes given bytes via Base64 encoding (with padding) +func Base64Encode(data []byte) string { + return base64.StdEncoding.EncodeToString(data) +} + +// Decodes given bytes via Base64 encoding (with padding) +func Base64Decode(encodedBytes []byte) string { + decodedBytes := make([]byte, len(encodedBytes)) + base64.StdEncoding.Decode(decodedBytes, encodedBytes) + return string(decodedBytes) +} + +// Encodes given string via Base64 encoding (with padding) +func Base64EncodeStr(str string) string { + return base64.StdEncoding.EncodeToString([]byte(str)) +} + +// Decodes given string via Base64 encoding (with padding) +func Base64DecodeStr(encodedStr string) string { + decodedBytes, _ := base64.StdEncoding.DecodeString(encodedStr) + return string(decodedBytes) +} + +// Encrypts given data using AES encryption with key of 16, 24 or 32 bytes +func Encrypt(key, data []byte) ([]byte, error) { + block, err := aes.NewCipher(key) + if err != nil { + return nil, err + } + + aesGCM, err := cipher.NewGCM(block) + if err != nil { + return nil, err + } + + nonce := make([]byte, aesGCM.NonceSize()) + encryptedData := aesGCM.Seal(nonce, nonce, data, nil) + + return encryptedData, nil +} + +// Decrypts data encrypted with AES encryption with given key of 16, 24 or 32 bytes +func Decrypt(key, data []byte) ([]byte, error) { + block, err := aes.NewCipher(key) + if err != nil { + return nil, err + } + + aesGCM, err := cipher.NewGCM(block) + if err != nil { + return nil, err + } + + nonce, encryptedBytes := data[:aesGCM.NonceSize()], data[aesGCM.NonceSize():] + decryptedData, err := aesGCM.Open(nil, nonce, encryptedBytes, nil) + if err != nil { + return nil, err + } + + return decryptedData, nil +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..8e25103 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module unbewohnte/goauxilib + +go 1.18 diff --git a/localIP.go b/localIP.go new file mode 100644 index 0000000..6a94ab8 --- /dev/null +++ b/localIP.go @@ -0,0 +1,28 @@ +/* +The MIT License (MIT) + +Copyright © 2022 Kasyanov Nikolay Alexeyevich (Unbewohnte) + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +*/ + +package goauxilib + +import "net" + +// Get local IP address. If an error occurs - returns an empty string +func GetLocalIPAddress() (string, error) { + conn, err := net.Dial("udp", "8.8.8.8:80") + if err != nil { + return "", err + } + defer conn.Close() + + localAddr := conn.LocalAddr().(*net.UDPAddr) + + return localAddr.IP.String(), nil +} diff --git a/logger.go b/logger.go new file mode 100644 index 0000000..e7e18d0 --- /dev/null +++ b/logger.go @@ -0,0 +1,57 @@ +/* +The MIT License (MIT) + +Copyright © 2022 Kasyanov Nikolay Alexeyevich (Unbewohnte) + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +*/ + +package goauxilib + +import ( + "io" + "log" + "os" +) + +// 3 basic loggers in global space +var ( + // neutral information logger + infoLog *log.Logger + // warning-level information logger + warningLog *log.Logger + // error-level information logger + errorLog *log.Logger +) + +func init() { + infoLog = log.New(os.Stdout, "[INFO] ", log.Ldate|log.Ltime) + warningLog = log.New(os.Stdout, "[WARNING] ", log.Ldate|log.Ltime) + errorLog = log.New(os.Stdout, "[ERROR] ", log.Ldate|log.Ltime) +} + +// Set up loggers to write to the given writer +func SetOutput(writer io.Writer) { + infoLog.SetOutput(writer) + warningLog.SetOutput(writer) + errorLog.SetOutput(writer) +} + +// Log information +func Info(format string, a ...interface{}) { + infoLog.Printf(format, a...) +} + +// Log warning +func Warning(format string, a ...interface{}) { + warningLog.Printf(format, a...) +} + +// Log error +func Error(format string, a ...interface{}) { + errorLog.Printf(format, a...) +} diff --git a/sha.go b/sha.go new file mode 100644 index 0000000..1ec2513 --- /dev/null +++ b/sha.go @@ -0,0 +1,41 @@ +/* +The MIT License (MIT) + +Copyright © 2022 Kasyanov Nikolay Alexeyevich (Unbewohnte) + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +*/ + +package goauxilib + +import ( + "crypto/sha1" + "crypto/sha256" + "crypto/sha512" + "fmt" +) + +// Returns HEX'ed string of SHA1'd data +func Sha1Hex(data []byte) string { + hash := sha1.New() + hash.Write(data) + return fmt.Sprintf("%x", hash.Sum(nil)) +} + +// Returns HEX'ed string of SHA256'd data +func Sha256Hex(data []byte) string { + hash := sha256.New() + hash.Write(data) + return fmt.Sprintf("%x", hash.Sum(nil)) +} + +// Returns HEX'ed string of SHA512'd data +func Sha512Hex(data []byte) string { + hash := sha512.New() + hash.Write(data) + return fmt.Sprintf("%x", hash.Sum(nil)) +} diff --git a/verification.go b/verification.go new file mode 100644 index 0000000..48c86e1 --- /dev/null +++ b/verification.go @@ -0,0 +1,28 @@ +/* +The MIT License (MIT) + +Copyright © 2022 Kasyanov Nikolay Alexeyevich (Unbewohnte) + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +*/ + +package goauxilib + +import ( + "fmt" + "net/mail" +) + +// Checks if string looks like a valid email. If not - returns reason why it is not the case +func DoesLookLikeEmail(email string) (bool, string) { + _, err := mail.ParseAddress(email) + if err != nil { + return false, fmt.Sprintf("%s", err) + } + + return true, "" +}