Benito Serna Tips and tools for Ruby on Rails developers

How to read the noCertificado from a CSD for a mexican CFDI

May 5, 2021

This was new for me, I was looking how to get the noCertificado from a CSD (Certificado de Sello Digital) and I found it is the serial of the certificate but in base 2.

In ruby you can get it with a OpenSSL::X509::Certificate

raw = File.read("my_cert.cer")
cert = OpenSSL::X509::Certificate.new(raw)

If you ask the serial you will get an OpenSSL::BN

cert.serial # => an OpenSSL::BN

And then you can ask the for the to_s of that object. By default it will return it with base 10

cert.serial.to_s == cert.serial.to_s(10) # => true

…but you can ask for it in base 2, this is the noCertificado =)

cert.serial.to_s(2) # => "noCertificato" value...

Related articles

Weekly tips and tools for Ruby on Rails developers

I send an email each week, trying to share knowledge and fixes to common problems and struggles for ruby on rails developers, like How to fetch the latest-N-of-each record or How to test that an specific mail was sent or a Capybara cheatsheet. You can see more examples on Most recent posts or All post by topic.