Skip to content

Instantly share code, notes, and snippets.

@Hexa
Last active October 28, 2016 16:35
Show Gist options
  • Save Hexa/200c9441ad1d942d860ea667b327d5af to your computer and use it in GitHub Desktop.
Save Hexa/200c9441ad1d942d860ea667b327d5af to your computer and use it in GitHub Desktop.
rsa
require "openssl"
#@[Link("crypto")]
lib LibCrypto
alias RSA = Void*
alias BIGNUM = Void*
RSA_3 = 0x3
RSA_F4 = 0x10001
fun pem_write_bio_rsaprivatekey = PEM_write_bio_RSAPrivateKey(bio: Bio*, rsa: RSA, enc: Void*, kstr: Void*, klen: Int, cp: Void*, u: Void*) : Int
fun pem_write_bio_rsapublickey = PEM_write_bio_RSAPublicKey(bio: Bio*, rsa: RSA) : Int
fun bn_new = BN_new() : BIGNUM
fun bn_set_word = BN_set_word(num: BIGNUM, word: UInt) : Int
fun rsa_new = RSA_new() : RSA
fun rsa_generate_key_ex = RSA_generate_key_ex(rsa: RSA, bits: Int, e: BIGNUM, cb: Void*) : Int
fun rsa_free = RSA_free(rsa: RSA) : Void
end
num = LibCrypto.bn_new()
LibCrypto.bn_set_word(num, LibCrypto::RSA_F4);
rsa = LibCrypto.rsa_new()
LibCrypto.rsa_generate_key_ex(rsa, 2048, num, nil)
bio = OpenSSL::BIO.new(io = MemoryIO.new)
LibCrypto.pem_write_bio_rsaprivatekey(bio, rsa, nil, nil, 0, nil, nil)
puts io.to_s
LibCrypto.bio_free(bio)
bio = OpenSSL::BIO.new(io = MemoryIO.new)
LibCrypto.pem_write_bio_rsapublickey(bio, rsa)
puts io.to_s
LibCrypto.bio_free(bio)
LibCrypto.rsa_free(rsa)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment