Documentation ¶
Overview ¶
Package crypto provides a set of cryptographic utilities in Go.
It includes interfaces and functions for handling cryptographic keys, such as Key, KeyGenerator, and KeyImporter. These interfaces provide methods for key generation, import, signing, verifying, encrypting, and decrypting.
The package supports a variety of cryptographic algorithms, including HMAC SHA, AES CBC, AES GCM, ECDSA, and RSA.
Example usage:
key, err := KeyImport[string]("123456", AesCbc128) if err != nil { panic(err) } ciphertext, err := key.Encrypt("hello world") if err != nil { panic(err) } plaintext, err := key.Decrypt(ciphertext) if err != nil { panic(err) }
This will encrypt the string "hello world" using the AES CBC 128 algorithm and then decrypt it back to the original string.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func KeyGenerate ¶
KeyGenerate is a function that generates a cryptographic key based on a given algorithm. It supports ECDSA and RSA algorithms. If the algorithm is not supported, it returns an error.
func KeyImport ¶
func KeyImport[T types.DataType](alg types.Algorithm, raw interface{}, opts ...key.Option[T]) (key.Key[T], error)
KeyImport is a function that imports a cryptographic key based on a given raw data and algorithm. It supports HMAC SHA, AES CBC, AES GCM, ECDSA, and RSA algorithms. If the algorithm is not supported, it returns an error.
Example ¶
utils.RandomSize = func(len int) ([]byte, error) { b := make([]byte, len) for i := 0; i < len; i++ { b[i] = 'a' } return b, nil } key, err := KeyImport[string](types.AesCbc128, "123456") if err != nil { panic(err) } ciphertext, err := key.Encrypt("hello world") if err != nil { panic(err) } fmt.Println(ciphertext) plaintext, err := key.Decrypt(ciphertext) if err != nil { panic(err) } fmt.Println(plaintext)
Output: aes_cbc_128.YWFhYWFhYWFhYWFhYWFhYWv1wt6aTe92jzFCoVBvNYU hello world
Types ¶
This section is empty.