Documentation
¶
Index ¶
- type HashType
- type Service
- func (s *Service) DecryptPGP(privateKey, ciphertext []byte) ([]byte, error)
- func (s *Service) DecryptRSA(privateKey, ciphertext, label []byte) ([]byte, error)
- func (s *Service) EncryptPGP(publicKey, data []byte) ([]byte, error)
- func (s *Service) EncryptRSA(publicKey, data, label []byte) ([]byte, error)
- func (s *Service) Fletcher16(payload string) uint16
- func (s *Service) Fletcher32(payload string) uint32
- func (s *Service) Fletcher64(payload string) uint64
- func (s *Service) GeneratePGPKeyPair(name, email, comment string) (publicKey, privateKey []byte, err error)
- func (s *Service) GenerateRSAKeyPair(bits int) (publicKey, privateKey []byte, err error)
- func (s *Service) Hash(lib HashType, payload string) string
- func (s *Service) IsHashAlgo(algo string) bool
- func (s *Service) Luhn(payload string) bool
- func (s *Service) SignPGP(privateKey, data []byte) ([]byte, error)
- func (s *Service) SymmetricallyEncryptPGP(passphrase, data []byte) ([]byte, error)
- func (s *Service) VerifyPGP(publicKey, data, signature []byte) error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type HashType ¶
type HashType string
HashType defines the supported hashing algorithms.
const ( // LTHN is a custom quasi-salted hashing algorithm. LTHN HashType = "lthn" // SHA512 is the SHA-512 hashing algorithm. SHA512 HashType = "sha512" // SHA256 is the SHA-256 hashing algorithm. SHA256 HashType = "sha256" // SHA1 is the SHA-1 hashing algorithm. SHA1 HashType = "sha1" // MD5 is the MD5 hashing algorithm. MD5 HashType = "md5" )
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service is the main struct for the crypt service. It provides methods for hashing, checksums, and encryption.
func NewService ¶
func NewService() *Service
NewService creates a new crypt Service and initialises its embedded services. It returns a new Service.
func (*Service) DecryptPGP ¶
DecryptPGP decrypts data with a private key. It returns the decrypted data.
Example ¶
package main
import (
"fmt"
"log"
"github.com/Snider/Enchantrix/pkg/crypt"
)
func main() {
cryptService := crypt.NewService()
publicKey, privateKey, err := cryptService.GeneratePGPKeyPair("test", "test@example.com", "test key")
if err != nil {
log.Fatalf("Failed to generate PGP key pair: %v", err)
}
message := []byte("This is a secret message for PGP.")
ciphertext, err := cryptService.EncryptPGP(publicKey, message)
if err != nil {
log.Fatalf("Failed to encrypt with PGP: %v", err)
}
decrypted, err := cryptService.DecryptPGP(privateKey, ciphertext)
if err != nil {
log.Fatalf("Failed to decrypt with PGP: %v", err)
}
fmt.Printf("Decrypted message: %s\n", decrypted)
}
Output: Decrypted message: This is a secret message for PGP.
func (*Service) DecryptRSA ¶
DecryptRSA decrypts data with a private key.
func (*Service) EncryptPGP ¶
EncryptPGP encrypts data with a public key. It returns the encrypted data.
Example ¶
package main
import (
"fmt"
"log"
"github.com/Snider/Enchantrix/pkg/crypt"
)
func main() {
cryptService := crypt.NewService()
publicKey, _, err := cryptService.GeneratePGPKeyPair("test", "test@example.com", "test key")
if err != nil {
log.Fatalf("Failed to generate PGP key pair: %v", err)
}
message := []byte("This is a secret message for PGP.")
ciphertext, err := cryptService.EncryptPGP(publicKey, message)
if err != nil {
log.Fatalf("Failed to encrypt with PGP: %v", err)
}
fmt.Printf("PGP ciphertext is not empty: %v\n", len(ciphertext) > 0)
}
Output: PGP ciphertext is not empty: true
func (*Service) EncryptRSA ¶
EncryptRSA encrypts data with a public key.
func (*Service) Fletcher16 ¶
Fletcher16 computes the Fletcher-16 checksum. It is a fast checksum algorithm that is more reliable than a simple sum.
Example ¶
package main
import (
"fmt"
"github.com/Snider/Enchantrix/pkg/crypt"
)
func main() {
cryptService := crypt.NewService()
fletcherPayload := "abcde"
fmt.Printf("Fletcher16 Checksum (Payload: \"%s\"): %d\n", fletcherPayload, cryptService.Fletcher16(fletcherPayload))
}
Output: Fletcher16 Checksum (Payload: "abcde"): 51440
func (*Service) Fletcher32 ¶
Fletcher32 computes the Fletcher-32 checksum. It provides better error detection than Fletcher-16.
Example ¶
package main
import (
"fmt"
"github.com/Snider/Enchantrix/pkg/crypt"
)
func main() {
cryptService := crypt.NewService()
fletcherPayload := "abcde"
fmt.Printf("Fletcher32 Checksum (Payload: \"%s\"): %d\n", fletcherPayload, cryptService.Fletcher32(fletcherPayload))
}
Output: Fletcher32 Checksum (Payload: "abcde"): 4031760169
func (*Service) Fletcher64 ¶
Fletcher64 computes the Fletcher-64 checksum. It provides the best error detection of the Fletcher algorithms.
Example ¶
package main
import (
"fmt"
"github.com/Snider/Enchantrix/pkg/crypt"
)
func main() {
cryptService := crypt.NewService()
fletcherPayload := "abcde"
fmt.Printf("Fletcher64 Checksum (Payload: \"%s\"): %d\n", fletcherPayload, cryptService.Fletcher64(fletcherPayload))
}
Output: Fletcher64 Checksum (Payload: "abcde"): 14467467625952928454
func (*Service) GeneratePGPKeyPair ¶
func (s *Service) GeneratePGPKeyPair(name, email, comment string) (publicKey, privateKey []byte, err error)
GeneratePGPKeyPair creates a new PGP key pair. It returns the public and private keys in PEM format.
Example ¶
package main
import (
"fmt"
"log"
"github.com/Snider/Enchantrix/pkg/crypt"
)
func main() {
cryptService := crypt.NewService()
publicKey, privateKey, err := cryptService.GeneratePGPKeyPair("test", "test@example.com", "test key")
if err != nil {
log.Fatalf("Failed to generate PGP key pair: %v", err)
}
fmt.Printf("PGP public key is not empty: %v\n", len(publicKey) > 0)
fmt.Printf("PGP private key is not empty: %v\n", len(privateKey) > 0)
}
Output: PGP public key is not empty: true PGP private key is not empty: true
func (*Service) GenerateRSAKeyPair ¶
GenerateRSAKeyPair creates a new RSA key pair.
func (*Service) Hash ¶
Hash computes a hash of the payload using the specified algorithm. It returns the hash as a hex-encoded string.
Example ¶
package main
import (
"fmt"
"github.com/Snider/Enchantrix/pkg/crypt"
)
func main() {
cryptService := crypt.NewService()
payload := "Enchantrix"
hashTypes := []crypt.HashType{
crypt.LTHN,
crypt.MD5,
crypt.SHA1,
crypt.SHA256,
crypt.SHA512,
}
fmt.Printf("Payload to hash: \"%s\"\n", payload)
for _, hashType := range hashTypes {
hash := cryptService.Hash(hashType, payload)
fmt.Printf(" - %-6s: %s\n", hashType, hash)
}
}
Output: Payload to hash: "Enchantrix" - lthn : 331f24f86375846ac8d0d06cfb80cb2877e8900548a88d4ac8d39177cd854dab - md5 : 7c54903a10f058a93fd1f21ea802cb27 - sha1 : 399f776c4b97e558a2c4f319b223dd481c6d43f1 - sha256: 2ae653f74554abfdb2343013925f5184a0f05e4c2e0c3881448fc80caeb667c2 - sha512: 9638018a9720b5d83fba7f3899e4ba5ab78018781f9c600f0c0738ff8ccf1ea54e1c783ee8778542b70aa26283d87ce88784b2df5697322546d3b8029c4b6797
func (*Service) IsHashAlgo ¶
IsHashAlgo checks if a string is a valid hash algorithm.
func (*Service) Luhn ¶
Luhn validates a number using the Luhn algorithm. It is typically used to validate credit card numbers.
Example ¶
package main
import (
"fmt"
"github.com/Snider/Enchantrix/pkg/crypt"
)
func main() {
cryptService := crypt.NewService()
luhnPayloadGood := "49927398716"
luhnPayloadBad := "49927398717"
fmt.Printf("Luhn Checksum:\n")
fmt.Printf(" - Payload '%s' is valid: %v\n", luhnPayloadGood, cryptService.Luhn(luhnPayloadGood))
fmt.Printf(" - Payload '%s' is valid: %v\n", luhnPayloadBad, cryptService.Luhn(luhnPayloadBad))
}
Output: Luhn Checksum: - Payload '49927398716' is valid: true - Payload '49927398717' is valid: false
func (*Service) SignPGP ¶
SignPGP creates a detached signature for a message. It returns the signature.
Example ¶
package main
import (
"fmt"
"log"
"github.com/Snider/Enchantrix/pkg/crypt"
)
func main() {
cryptService := crypt.NewService()
_, privateKey, err := cryptService.GeneratePGPKeyPair("test", "test@example.com", "test key")
if err != nil {
log.Fatalf("Failed to generate PGP key pair: %v", err)
}
message := []byte("This is a message to be signed.")
signature, err := cryptService.SignPGP(privateKey, message)
if err != nil {
log.Fatalf("Failed to sign with PGP: %v", err)
}
fmt.Printf("PGP signature is not empty: %v\n", len(signature) > 0)
}
Output: PGP signature is not empty: true
func (*Service) SymmetricallyEncryptPGP ¶
SymmetricallyEncryptPGP encrypts data with a passphrase. It returns the encrypted data.
Example ¶
package main
import (
"fmt"
"log"
"github.com/Snider/Enchantrix/pkg/crypt"
)
func main() {
cryptService := crypt.NewService()
passphrase := []byte("my secret passphrase")
message := []byte("This is a symmetric secret.")
ciphertext, err := cryptService.SymmetricallyEncryptPGP(passphrase, message)
if err != nil {
log.Fatalf("Failed to symmetrically encrypt with PGP: %v", err)
}
fmt.Printf("Symmetric PGP ciphertext is not empty: %v\n", len(ciphertext) > 0)
}
Output: Symmetric PGP ciphertext is not empty: true
func (*Service) VerifyPGP ¶
VerifyPGP verifies a detached signature for a message. It returns an error if the signature is invalid.
Example ¶
package main
import (
"fmt"
"log"
"github.com/Snider/Enchantrix/pkg/crypt"
)
func main() {
cryptService := crypt.NewService()
publicKey, privateKey, err := cryptService.GeneratePGPKeyPair("test", "test@example.com", "test key")
if err != nil {
log.Fatalf("Failed to generate PGP key pair: %v", err)
}
message := []byte("This is a message to be signed.")
signature, err := cryptService.SignPGP(privateKey, message)
if err != nil {
log.Fatalf("Failed to sign with PGP: %v", err)
}
err = cryptService.VerifyPGP(publicKey, message, signature)
if err != nil {
fmt.Println("PGP signature verification failed.")
} else {
fmt.Println("PGP signature verified successfully.")
}
}
Output: PGP signature verified successfully.