Skip to content

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
FunctionDescriptionInputOutput
generate_keygenerate a signed secret key using name, email, comment, key_type and key_lengthKeyParamsSignedSecretKey
SignedSecretKey.get_signed_public_keyget a signed public key from signed secret keySignedPublicKey
import_publickeyimport signed public key in armored formatstringSignedPublicKey
import_publickey_from_filelike import_publickey but from a filestringSignedPublicKey
import_secretkeyimport signed secret key in armored formatstringSignedSecretKey
import_secretkey_from_filelike import_secretkey but from a filestringSignedSecretKey
SignedPublicKey.sign_messagesign message with your signed public keystringSignature
SignedSecretKey.verify_signatureverify signature for message with your signed secret keySignature, stringbool
SignedPublicKey.encrypt_from_textencrypt message from text using your signed public keystring[]byte
SignedSecretKey.decrypt_to_textdecrypt encrypted messages to text using your signed secret key[]bytestring
Signature.to_bytesconvert signature to bytes[]byte
Signature.to_hexconvert signature to hexstring
signature_from_hexconvert hex string to signaturestringSignature

Notes:

  • key_type is enum of [cv25519, rsa], by default key_type will be cv25519
  • key_length used if key_type is rsa and 3072 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
  1. 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>) !
  1. 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
  1. Get signed public key from signed secret key
spk := ssk.get_signed_public_key() !
  1. 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