Benito Serna Tips and tools for Ruby on Rails developers

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

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