crypt.crpgp #
CRPGP
This module based on threefold/crpgp repo, which is a wrapper for rpgp
Install
All needed libs can be installed from install.sh
High Level API
- List of supported functions with a description for each method
Function | Description | Input | Output |
---|---|---|---|
generate_key | generate a signed secret key using name, email, comment, key_type and key_length | KeyParams | SignedSecretKey |
SignedSecretKey.get_signed_public_key | get a signed public key from signed secret key | SignedPublicKey | |
import_publickey | import signed public key in armored format | string | SignedPublicKey |
import_publickey_from_file | like import_publickey but from a file | string | SignedPublicKey |
import_secretkey | import signed secret key in armored format | string | SignedSecretKey |
import_secretkey_from_file | like import_secretkey but from a file | string | SignedSecretKey |
SignedPublicKey.sign_message | sign message with your signed public key | string | Signature |
SignedSecretKey.verify_signature | verify signature for message with your signed secret key | Signature, string | bool |
SignedPublicKey.encrypt_from_text | encrypt message from text using your signed public key | string | []byte |
SignedSecretKey.decrypt_to_text | decrypt encrypted messages to text using your signed secret key | []byte | string |
Signature.to_bytes | convert signature to bytes | []byte | |
Signature.to_hex | convert signature to hex | string | |
signature_from_hex | convert hex string to signature | string | Signature |
Notes:
- key_type is enum of [
cv25519
,rsa
], by default key_type will becv25519
- key_length used if key_type is
rsa
and3072
is a default value
How to use !
We will go together step by step to illustrate how to use this module.
Import crpgp module
import crpgp
- We have two options here, generate a new key, or import
- Generate:
- CV25519 key:
ssk := crpgp.generate_key(name: <YOUR_NAME>, email: <YOUR_EMAIL>) !
- RSA key
// key_length 3072 if not passed
ssk := crpgp.generate_key(name: <YOUR_NAME>, email: <YOUR_EMAIL>, key_type: .rsa) !
or
ssk := crpgp.generate_key(name: <YOUR_NAME>, email: <YOUR_EMAIL>, key_type: .rsa, key_length: <LENGTH>) !
- Import signed secret key from file
ssk := crpgp.import_secretkey_from_file(<FILE_PATH>) !
- Now we have a signed secret key, let us get a signed public key, also we have two options, get from secret key or import
- Get signed public key from signed secret key
spk := ssk.get_signed_public_key() !
- import signed public key from file
spk := crpgp.import_publickey_from_file(<FILE_PATH>) !
- Now we have a signed public and secret key, let us try to sign and verify a message
message := 'these are my secrets\n12345'
mut sig := ssk.sign_message(message) !
println(sig.to_hex() !) // In case we want to convert it to hex, here just to display it
if spk.verify_signature(sig, message) {
println('✅ message signed and verified!')
}
- let us try encrypt and decrypt message.
encrypted := spk.encrypt_from_text(message) !
decrypted := ssk.decrypt_to_text(encrypted) !
if message == decrypted {
println('✅ message encrypted and decrypted!')
}
- Done !
fn cu8_to_vbytes #
fn cu8_to_vbytes(ptr &u8, l u64) []byte
fn deserialize_signature #
fn deserialize_signature(bytes []byte) !Signature
fn generate_key #
fn generate_key(k KeyParams) !SignedSecretKey
fn import_publickey #
fn import_publickey(data string) !SignedPublicKey
import functions import public key from ASCII armor text format
fn import_publickey_from_file #
fn import_publickey_from_file(path string) !SignedPublicKey
import public key from ASCII armor file format
fn import_secretkey #
fn import_secretkey(data string) !SignedSecretKey
import secret key from ASCII armor text format
fn import_secretkey_from_file #
fn import_secretkey_from_file(path string) !SignedSecretKey
import secret key from ASCII armor file format
fn new_secret_key_param_builder #
fn new_secret_key_param_builder() !SecretKeyParamsBuilder
fn new_subkey_params_builder #
fn new_subkey_params_builder() SubkeyParamsBuilder
fn signature_from_hex #
fn signature_from_hex(sig string) !Signature
fn signed_public_key_from_armored #
fn signed_public_key_from_armored(s string) !SignedPublicKey
fn signed_public_key_from_bytes #
fn signed_public_key_from_bytes(bytes []byte) !SignedPublicKey
fn signed_secret_key_from_armored #
fn signed_secret_key_from_armored(s string) !SignedSecretKey
fn signed_secret_key_from_bytes #
fn signed_secret_key_from_bytes(bytes []byte) !SignedSecretKey
fn str_to_bytes #
fn str_to_bytes(s string) []byte
fn (PublicKey) verify #
fn (p &PublicKey) verify(data []byte, sig &Signature) !
fn (PublicKey) sign_and_free #
fn (p &PublicKey) sign_and_free(sk SignedSecretKey) !SignedPublicKey
fn (PublicKey) encrypt #
fn (s &PublicKey) encrypt(data []byte) ![]byte
fn (SecretKey) sign #
fn (s &SecretKey) sign() !SignedSecretKey
fn (SecretKeyParams) generate_and_free #
fn (s &SecretKeyParams) generate_and_free() !SecretKey
fn (SecretKeyParamsBuilder) primary_key_id #
fn (b &SecretKeyParamsBuilder) primary_key_id(primary_key_id string) !
fn (SecretKeyParamsBuilder) key_type #
fn (b &SecretKeyParamsBuilder) key_type(key_type C.KeyType) !
fn (SecretKeyParamsBuilder) subkey #
fn (b &SecretKeyParamsBuilder) subkey(subkey SubkeyParams) !
fn (SecretKeyParamsBuilder) build #
fn (b &SecretKeyParamsBuilder) build() !SecretKeyParams
fn (Signature) serialize #
fn (s &Signature) serialize() ![]byte
fn (Signature) to_hex #
fn (sig &Signature) to_hex() !string
fn (SubkeyParamsBuilder) key_type #
fn (b &SubkeyParamsBuilder) key_type(key_type C.KeyType) !
fn (SubkeyParamsBuilder) build #
fn (b &SubkeyParamsBuilder) build() !SubkeyParams
enum CipherSuite #
enum CipherSuite {
cv25519 // ed_dsa primary, ecdh sub
rsa // rsa primary (rsa and bitsize can be parameterized)
}
enum KeyType_Tag #
enum KeyType_Tag {
rsa
ecdh
ed_dsa
}
Things that are hard to convert from the c header file automatically
struct KeyParams #
@[params]
struct KeyParams {
name string @[required]
email string @[required]
key_type CipherSuite
length u32 = 3072
comment string
}
KeyParams
struct SignedPublicKey #
struct SignedPublicKey {
internal &C.SignedPublicKey
}
fn (SignedPublicKey) encrypt #
fn (s &SignedPublicKey) encrypt(data []byte) ![]byte
fn (SignedPublicKey) encrypt_from_text #
fn (spk &SignedPublicKey) encrypt_from_text(message string) ![]byte
fn (SignedPublicKey) encrypt_with_any #
fn (s &SignedPublicKey) encrypt_with_any(data []byte) ![]byte
fn (SignedPublicKey) to_armored #
fn (s &SignedPublicKey) to_armored() !string
fn (SignedPublicKey) to_bytes #
fn (s &SignedPublicKey) to_bytes() ![]byte
fn (SignedPublicKey) verify #
fn (p &SignedPublicKey) verify(data []byte, sig &Signature) !
fn (SignedPublicKey) verify_signature #
fn (spk &SignedPublicKey) verify_signature(sig &Signature, message string) bool
Public Key Functions
struct SignedSecretKey #
struct SignedSecretKey {
internal &C.SignedSecretKey
}
fn (SignedSecretKey) create_signature #
fn (s &SignedSecretKey) create_signature(data []byte) !Signature
fn (SignedSecretKey) decrypt #
fn (s &SignedSecretKey) decrypt(data []byte) ![]byte
fn (SignedSecretKey) decrypt_to_text #
fn (ssk &SignedSecretKey) decrypt_to_text(encrypted_message []byte) !string
fn (SignedSecretKey) get_signed_public_key #
fn (ssk &SignedSecretKey) get_signed_public_key() !SignedPublicKey
fn (SignedSecretKey) public_key #
fn (s &SignedSecretKey) public_key() !PublicKey
fn (SignedSecretKey) sign_message #
fn (ssk &SignedSecretKey) sign_message(message string) !Signature
Secret Key Fucntions
fn (SignedSecretKey) to_armored #
fn (s &SignedSecretKey) to_armored() !string
fn (SignedSecretKey) to_bytes #
fn (s &SignedSecretKey) to_bytes() ![]byte
- README
- fn cu8_to_vbytes
- fn deserialize_signature
- fn generate_key
- fn import_publickey
- fn import_publickey_from_file
- fn import_secretkey
- fn import_secretkey_from_file
- fn new_secret_key_param_builder
- fn new_subkey_params_builder
- fn signature_from_hex
- fn signed_public_key_from_armored
- fn signed_public_key_from_bytes
- fn signed_secret_key_from_armored
- fn signed_secret_key_from_bytes
- fn str_to_bytes
- type PublicKey
- type SecretKey
- type SecretKeyParams
- type SecretKeyParamsBuilder
- type Signature
- type SubkeyParamsBuilder
- enum CipherSuite
- enum KeyType_Tag
- struct KeyParams
- struct SignedPublicKey
- struct SignedSecretKey