johninvictus
Hi Guys
Help me rewrite this Javascript function to Elixir.
security: function security() {
//sandbox value = Security Credential (Shortcode 1)
//production value = api initiator password
var bufferToEncrypt = new Buffer("Safaricom133!");
//read the sandbox/production certificate data
// PATH - e.g "./keys/sandbox-cert.cer"
var data = fs.readFileSync("PATH TO CERTIFICATE FILE");
//convert data to string
var privateKey = String(data);
//encrypt the credential using the privatekey
var encrypted = crypto.publicEncrypt({
key: privateKey,
padding: constants.RSA_PKCS1_PADDING
}, bufferToEncrypt);
//convert encrypted value to string and encode to base64
var securityCredential = encrypted.toString("base64");
//return value to invoking method
return securityCredential;
}
The certificate file looks something like this.
-----BEGIN CERTIFICATE-----
MIIGgDCCBWigAwIBAgIKMvrulAAAAARG5DANBgkqhkiG9w0BAQsFADBbMRMwEQYK
CZImiZPyLGQBGRYDbmV0MRkwFwYKCZImiZPyLGQBGRYJc2FmYXJpY29tMSkwJwYD
VQQDEyBTYWZhcmljb20gSW50ZXJuYWwgSXNzdWluZyBDQSAwMjAeFw0xNDExMTIw
NzEyNDVaFw0xNjExMTEwNzEyNDVaMHsxCzAJBgNVBAYTAktFMRAwDgYDVQQIEwdO
-----END CERTIFICATE-----
I have tried writing it but the task has proven to be tough.
Please help
Trending in Questions
I’m aware of 2 such projects, are there others?
git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
Hi everyone.
My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
Hello folks!
So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
How Can I Optimise Compile Time Dependencies
I have been building an elixir application for about 2 years now. Many modules and files ha...
New
Other Trending Topics
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with.
But I don’t use it anymore.
Serverless was the promis...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #metaprogramming










Showing Posts 1 to 8- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
cmkarlsson
Assuming this is what you want. RSA with RSA_PKCS1_PADDING has known flaws which is exploitable though the Bleichenbacker’s CCA attack. The improved version claims to crack RSA with as little as 15,000 attempts (assuming it has access to an oracle of some sort).
Anyway what you are trying to do is:
You can use the
public_keymodule to achieve this. From memory you do something like this:end
johninvictus
@cmkarlsson
Thank you for the answer.
The issue here is that the above certificate is not a pem file but cer file. This is what I get after I pem decode it.
Here is my full implementation.
Here is the error am getting.
It is not my choice am trying to write an Elixir API wrapper for MPesa (Safaricom Mobile Money Transfer Service)(M-Pesa - Wikipedia).
cmkarlsson
Ok, this means that
pem_entry_decodereturns a certificate and not a public_key directly. You then need to extract the public_key from the certificate data structure. I have not got anything available to test this right now and can’t remember from heart.The public key user guide might help you:
It is in erlang and they return erlang records, so you may want to read up on elixir Records as well to handle them in the proper way.
johninvictus
@cmkarlsson
Sorry am just 1 yr old at this elixir erlang game
Here is :term am getting from
pem_entry_decodeI can`t see any public key attribute anywhere.
cmkarlsson
It is hidden in the
SubjectPublicKeyInfo, there should be a subjectPublicKey in there somewhereThe
<<48, 130, 1, 10 ...>>is the DER encoded public key I think. So thenpublic_key:der_decode(:"RSAPublicKey", DerValue)johninvictus
@cmkarlsson
.
Thanks alot, your solution worked wonderfully
To extract the public key from the certificate record was hard but I have managed to do it using trial and error hacks. (If you know a better way please advice) .
Here is my final working solution incase someone else needs help.
Regards.
cmkarlsson
Great! I’d say the proper way to do it is to use the erlang records and access the information that way.
I’d define the Records in a separate module and then require them from the module where you want to use them.
Something like:
johninvictus
@cmkarlsson
Thank you very much, everything works better now. This was a great learning experience
Regards.