Created
February 5, 2024 19:38
-
-
Save spytheman/73aa06c27a8c6c728f906008265878bf to your computer and use it in GitHub Desktop.
Encoding and Decoding data with RSA keys, using Vlang's net.mbedtls wrapper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import net.mbedtls | |
const x = mbedtls.is_used // just silence a V warning, since we are using only the C functions from net.mbedtls, but not any pub V fn etc | |
type FnRNG = fn(p_rng voidptr, output &u8, output_len usize) int | |
fn C.mbedtls_pk_parse_public_keyfile(pk &C.mbedtls_pk_context, path &u8) int | |
fn C.mbedtls_pk_encrypt(pk &C.mbedtls_pk_context, const_input &u8, ilen usize, output &u8, olen &usize, osize usize, f_rng FnRNG, p_rng voidptr) int | |
fn C.mbedtls_pk_decrypt(pk &C.mbedtls_pk_context, const_input &u8, ilen usize, output &u8, olen &usize, osize usize, f_rng FnRNG, p_rng voidptr) int | |
fn encode_data(input string, pub_key_path string) ![]u8 { | |
rng_ctx := C.mbedtls_ctr_drbg_context{} | |
C.mbedtls_ctr_drbg_init(&rng_ctx) | |
defer { C.mbedtls_ctr_drbg_free(&rng_ctx) } | |
entropy := C.mbedtls_entropy_context{} | |
C.mbedtls_entropy_init(&entropy) | |
defer { C.mbedtls_entropy_free(&entropy) } | |
ret_seed := C.mbedtls_ctr_drbg_seed(&rng_ctx, C.mbedtls_entropy_func, &entropy, 0, 0) | |
if ret_seed != 0 { return error('> failed, mbedtls_ctr_drbg_seed returned ${ret_seed.hex()}') } | |
pk := C.mbedtls_pk_context{} | |
C.mbedtls_pk_init(&pk) | |
defer { C.mbedtls_pk_free(&pk) } | |
ret_pub := C.mbedtls_pk_parse_public_keyfile( &pk, pub_key_path.str ) | |
if ret_pub != 0 { return error('> failed, mbedtls_pk_parse_public_keyfile returned ${ret_pub.hex()}') } | |
buf := [1024]u8{} | |
olen := usize(0) | |
res_encrypt := C.mbedtls_pk_encrypt( &pk, &u8(input.str), usize(input.len), &buf[0], &olen, sizeof(buf), C.mbedtls_ctr_drbg_random, &rng_ctx ) | |
if res_encrypt != 0 { return error('> failed, mbedtls_pk_encrypt returned ${res_encrypt.hex()}') } | |
return buf[..olen] | |
} | |
fn decode_data(data []u8, private_key_path string, password string) !string { | |
rng_ctx := C.mbedtls_ctr_drbg_context{} | |
C.mbedtls_ctr_drbg_init(&rng_ctx) | |
defer { C.mbedtls_ctr_drbg_free(&rng_ctx) } | |
entropy := C.mbedtls_entropy_context{} | |
C.mbedtls_entropy_init(&entropy) | |
defer { C.mbedtls_entropy_free(&entropy) } | |
ret_seed := C.mbedtls_ctr_drbg_seed(&rng_ctx, C.mbedtls_entropy_func, &entropy, 0, 0) | |
if ret_seed != 0 { return error('> failed, mbedtls_ctr_drbg_seed returned ${ret_seed.hex()}') } | |
pk := C.mbedtls_pk_context{} | |
C.mbedtls_pk_init(&pk) | |
defer { C.mbedtls_pk_free(&pk) } | |
ret := C.mbedtls_pk_parse_keyfile(&pk, private_key_path.str, password.str, C.mbedtls_ctr_drbg_random, &rng_ctx) | |
if ret != 0 { return error('> failed, mbedtls_pk_parse_keyfile returned ${ret.hex()}') } | |
buf := [1024]u8{} | |
olen := usize(0) | |
res_decrypt := C.mbedtls_pk_decrypt( &pk, &u8(data.data), usize(data.len), &buf[0], &olen, sizeof(buf), C.mbedtls_ctr_drbg_random, &rng_ctx ) | |
if res_decrypt != 0 { return error('> failed, mbedtls_pk_decrypt returned ${res_decrypt.hex()}') } | |
return unsafe{(&buf[0]).vstring_with_len(int(olen))}.clone() | |
} | |
fn main() { | |
unbuffer_stdout() | |
input := 'hello world' | |
dump(input) | |
encoded := encode_data(input, os.resource_abs_path('our-key.pub'))! | |
dump(encoded) | |
dump(encoded.len) | |
output := decode_data(encoded, os.resource_abs_path('our-key.pem'), '')! | |
dump(output) | |
assert input == output | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
openssl genrsa -out our-key.pem 2048 | |
openssl rsa -pubout -in our-key.pem -out our-key.pub |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-----BEGIN RSA PRIVATE KEY----- | |
MIIEowIBAAKCAQEAuna7nzwFEXpMbBVWK+twjSVO68UH/fdaXntkoDhxpOSanIOi | |
xcN3CgtAuIKvQEuonMZtYXsmWKH1NobygCU95JkwW5j8G0991yQmveXFxpgYT5Si | |
mkNimKxbHxNpXZjXoK+PR/uvvF/zAIz1dbb7IxaDKmv2isqot8g0yPyJeNnpYL40 | |
29SwoGtLtxvbL2hgB7Gz/czQKz6FSFlSha69LimefO3CxpBlA8dQUgH4iLSr/hur | |
QpYYeWRn/c0dOrkcrxFAebd299QyVpEEVyl51Z1T3rAjvmOhb6Rn20e4ztRREiRO | |
fjlKGoVkar1mGkeCq16b5DHLt4SszjHr5z7NwwIDAQABAoIBAC95d3+tWmT+zGJg | |
dFYuVbO6IgPpYIWK6tlPdWlNtbGT+sGKynCrua02/D1+GHy4Rq/T3ZmKL1M1uScu | |
6KMz2DRRxHwt0VJqxNF4BN0omVhc9SOd5mB8c6gHihO5b4RDG/Nvf32GWVIO4+Hq | |
Abz836gpXS/52QWxX4+bS8U4dw/ri3tnNGs3A+a+ecdtafNzUKwfaydMtBA6Uy0r | |
HP1pVyrzQriY/nNPasFriiRNM3ZGOn1Fd0uL3E2qm02KaZEZXuOdEExSWARDNfds | |
kZgzLOGJver6EkE9o8WeuMAF1TK/9Mp8XnYr4b6/rUQxuvBbA/9NsgXd9biepMp8 | |
oD0YrnECgYEA9XYVuP9iSJ4v8Sf+4TxFLK6nAqygk3p6j/t1nAn6/0ZNp0CeozjY | |
LEqaZzF3LcJefu3A+PJ9hTGp6N6fVfC7O2ZYi1g95FCgTEZz8xTePhZpSootKKAZ | |
lv+tbKMzAwyAMHt6B3xYtsoEt+IZCpSBQrM6AQBpbJ4RqbmY3Bvo4T0CgYEAwngx | |
3FOSi+HqVsFyr/yxDX3sS6YMTeu8JI/mWWjLotZshmlEH0XT/W2gOrgp9KrBj0rW | |
5fhKSU6uDfSX3j+bI0Voh9G/OKV/CpFRPveI7FMfjDAmmhrS0PoRVWOc9fEio/ET | |
BPdY4+moSYw/W5Ul40ROcC8NQ6id+q+vkqrjWv8CgYA3GiMWPH5bvucUQ947wcPl | |
m+9x8j653KzW1aQ5OyRVWfrkqOTzzzb3+mde8wPGCfQcmlLPj+ANnnB+DjvGlxvw | |
4bDO1yhNsKvLc6Vj1DNvzKgmS46HqCkWdJ8B3ED/VjcQqydhttBTeFZG0UK91UiP | |
KJzEzMrdzz4306o0/y7wIQKBgHV0oTDg7S89mQsjNgDjT5DFkJW0SdQ6YF146Hjy | |
GaRVfCDGrZVx2HNF5jPxDa4wAE5qtlcZ2sxiEDBvndZndm0cO+Fkpukw1AVu9bJh | |
UbrjDbffUUFcmB4OG7AGRUQqi3CXZ5RlTLQDaVhas929vK+Cjxmk4Y8O5bQGrrWY | |
L0/DAoGBAMxjq5uFUQ9/2amPq65896TjdxXhAYo6n2855AhEkU7v29fylMN+WQ5F | |
gC4uFWzh6ymWIDr1B24FXEdTpK3U+JetfwqDAicV2LRN3RMOP/Nlw730mZEr+Nl5 | |
J6onNLWI+pCdEteowBXg2q46USpAkOiATHulVZuHQfr2LDWStZXe | |
-----END RSA PRIVATE KEY----- |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-----BEGIN PUBLIC KEY----- | |
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuna7nzwFEXpMbBVWK+tw | |
jSVO68UH/fdaXntkoDhxpOSanIOixcN3CgtAuIKvQEuonMZtYXsmWKH1NobygCU9 | |
5JkwW5j8G0991yQmveXFxpgYT5SimkNimKxbHxNpXZjXoK+PR/uvvF/zAIz1dbb7 | |
IxaDKmv2isqot8g0yPyJeNnpYL4029SwoGtLtxvbL2hgB7Gz/czQKz6FSFlSha69 | |
LimefO3CxpBlA8dQUgH4iLSr/hurQpYYeWRn/c0dOrkcrxFAebd299QyVpEEVyl5 | |
1Z1T3rAjvmOhb6Rn20e4ztRREiROfjlKGoVkar1mGkeCq16b5DHLt4SszjHr5z7N | |
wwIDAQAB | |
-----END PUBLIC KEY----- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment