Crypto

package
v0.0.0-...-fa94a0d Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2025 License: MIT Imports: 26 Imported by: 0

Documentation

Overview

The Crypto class provides access to advanced cryptographic functionalities.

Currently, this includes asymmetric key encryption/decryption, signing/verification, and generating cryptographically secure random bytes, RSA keys, HMAC digests, and self-signed X509Certificates.

package main

import (
	"crypto/sha256"

	"graphics.gd/classdb/Crypto"
	"graphics.gd/classdb/HashingContext"
)

func ExampleCrypto() {
	crypto := Crypto.New()

	// Generate new RSA key.
	key := crypto.GenerateRsa(4096)
	// Generate new self-signed certificate with the given key.
	cert := crypto.MoreArgs().GenerateSelfSignedCertificate(key, "CN=mydomain.com,O=My Game Company,C=IT", "20140101000000", "20340101000000")
	// Save key and certificate in the user folder.
	key.Save("user://generated.key")
	cert.Save("user://generated.crt")
	// Encryption
	data := "Some data"
	encrypted := crypto.Encrypt(key, []byte(data))
	// Decryption
	decrypted := crypto.Decrypt(key, encrypted)
	// Signing
	var hash = sha256.Sum256([]byte(data))
	signature := crypto.Sign(HashingContext.HashSha256, hash[:], key)
	// Verifying
	verified := crypto.Verify(HashingContext.HashSha256, hash[:], signature, key)
	// Checks
	if !verified {
		panic("verification failed")
	}
	if string(decrypted) != data {
		panic("decryption failed")
	}
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Advanced

type Advanced = class

Advanced exposes a 1:1 low-level instance of the class, undocumented, for those who know what they are doing.

type Any

type Any interface {
	gd.IsClass
	AsCrypto() Instance
}

type Expanded

type Expanded = MoreArgs

type Extension

type Extension[T gdclass.Interface] struct{ gdclass.Extension[T, Instance] }

Extension can be embedded in a new struct to create an extension of this class. T should be the type that is embedding this Extension

func (*Extension[T]) AsCrypto

func (self *Extension[T]) AsCrypto() Instance

func (*Extension[T]) AsObject

func (self *Extension[T]) AsObject() [1]gd.Object

func (*Extension[T]) AsRefCounted

func (self *Extension[T]) AsRefCounted() [1]gd.RefCounted

type ID

type ID Object.ID

ID is a typed object ID (reference) to an instance of this class, use it to store references to objects with unknown lifetimes, as an ID will not panic on use if the underlying object has been destroyed.

func (ID) Instance

func (id ID) Instance() (Instance, bool)

type Instance

type Instance [1]gdclass.Crypto

Instance of the class with convieniently typed arguments and results.

var Nil Instance

Nil is a nil/null instance of the class. Equivalent to the zero value.

func New

func New() Instance

func (Instance) AsCrypto

func (self Instance) AsCrypto() Instance

func (Instance) AsObject

func (self Instance) AsObject() [1]gd.Object

func (Instance) AsRefCounted

func (self Instance) AsRefCounted() [1]gd.RefCounted

func (Instance) ConstantTimeCompare

func (self Instance) ConstantTimeCompare(trusted []byte, received []byte) bool

Compares two []bytes for equality without leaking timing information in order to prevent timing attacks.

See this blog post for more information.

func (Instance) Decrypt

func (self Instance) Decrypt(key CryptoKey.Instance, ciphertext []byte) []byte

Decrypt the given 'ciphertext' with the provided private 'key'.

Note: The maximum size of accepted ciphertext is limited by the key size.

func (Instance) Encrypt

func (self Instance) Encrypt(key CryptoKey.Instance, plaintext []byte) []byte

Encrypt the given 'plaintext' with the provided public 'key'.

Note: The maximum size of accepted plaintext is limited by the key size.

func (Instance) GenerateRandomBytes

func (self Instance) GenerateRandomBytes(size int) []byte

Generates a []byte of cryptographically secure random bytes with given 'size'.

func (Instance) GenerateRsa

func (self Instance) GenerateRsa(size int) CryptoKey.Instance

Generates an RSA CryptoKey that can be used for creating self-signed certificates and passed to StreamPeerTLS.AcceptStream.

func (Instance) GenerateSelfSignedCertificate

func (self Instance) GenerateSelfSignedCertificate(key CryptoKey.Instance) X509Certificate.Instance

Generates a self-signed X509Certificate from the given CryptoKey and 'issuer_name'. The certificate validity will be defined by 'not_before' and 'not_after' (first valid date and last valid date). The 'issuer_name' must contain at least "CN=" (common name, i.e. the domain name), "O=" (organization, i.e. your company name), "C=" (country, i.e. 2 lettered ISO-3166 code of the country the organization is based in).

A small example to generate an RSA key and an X509 self-signed certificate.

func (Instance) HmacDigest

func (self Instance) HmacDigest(hash_type HashingContext.HashType, key []byte, msg []byte) []byte

Generates an HMAC digest of 'msg' using 'key'. The 'hash_type' parameter is the hashing algorithm that is used for the inner and outer hashes.

Currently, only [Hashingcontext.HashSha256] and [Hashingcontext.HashSha1] are supported.

func (Instance) ID

func (self Instance) ID() ID

func (Instance) MoreArgs

func (self Instance) MoreArgs() MoreArgs

MoreArgs enables certain functions to be called with additional 'optional' arguments.

func (*Instance) SetObject

func (self *Instance) SetObject(obj [1]gd.Object) bool

func (Instance) Sign

func (self Instance) Sign(hash_type HashingContext.HashType, hash []byte, key CryptoKey.Instance) []byte

Sign a given 'hash' of type 'hash_type' with the provided private 'key'.

func (Instance) Verify

func (self Instance) Verify(hash_type HashingContext.HashType, hash []byte, signature []byte, key CryptoKey.Instance) bool

Verify that a given 'signature' for 'hash' of type 'hash_type' against the provided public 'key'.

func (Instance) Virtual

func (self Instance) Virtual(name string) reflect.Value

type MoreArgs

type MoreArgs [1]gdclass.Crypto

MoreArgs is a container for Instance functions with additional 'optional' arguments.

func (MoreArgs) GenerateSelfSignedCertificate

func (self MoreArgs) GenerateSelfSignedCertificate(key CryptoKey.Instance, issuer_name string, not_before string, not_after string) X509Certificate.Instance

Generates a self-signed X509Certificate from the given CryptoKey and 'issuer_name'. The certificate validity will be defined by 'not_before' and 'not_after' (first valid date and last valid date). The 'issuer_name' must contain at least "CN=" (common name, i.e. the domain name), "O=" (organization, i.e. your company name), "C=" (country, i.e. 2 lettered ISO-3166 code of the country the organization is based in).

A small example to generate an RSA key and an X509 self-signed certificate.

Jump to

Keyboard shortcuts

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