Class: BuhoCfdi::Key

Inherits:
Object
  • Object
show all
Defined in:
lib/buho_cfdi/key.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, password = nil) ⇒ Key

Returns a new instance of Key.



8
9
10
11
12
# File 'lib/buho_cfdi/key.rb', line 8

def initialize(file, password=nil)
  if file.is_a? String
    @private_key = file
  end
end

Instance Attribute Details

#private_keyObject (readonly)

Returns the value of attribute private_key.



6
7
8
# File 'lib/buho_cfdi/key.rb', line 6

def private_key
  @private_key
end

Instance Method Details

#generate_seal(xml) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/buho_cfdi/key.rb', line 21

def generate_seal(xml)
  chain       = original_chain(xml)
  key         = OpenSSL::PKey::RSA.new(@private_key)
  digester    = OpenSSL::Digest::SHA256.new
  signature   = key.sign(digester, chain)
  signature   = Base64.strict_encode64(signature)
  signature
end

#original_chain(invoice) ⇒ Object



30
31
32
33
34
35
# File 'lib/buho_cfdi/key.rb', line 30

def original_chain(invoice)
    invoice   = invoice
    xslt      = Nokogiri::XSLT(File.read('storage/xslt/cadenaoriginal_3_3.xslt'))
    chain     = xslt.transform(invoice)
    chain.text.gsub("\n","")
end

#seal(xml) ⇒ Object



14
15
16
17
18
19
# File 'lib/buho_cfdi/key.rb', line 14

def seal(xml)
  xml = Nokogiri::XML(xml)
  signature = generate_seal(xml)
  xml.at_css('cfdi|Comprobante').set_attribute('Sello',signature)
  xml.to_xml
end