nfe-signer 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +2 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/.travis.yml +7 -0
- data/Gemfile +4 -0
- data/README.md +78 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/nfe/signer.rb +21 -0
- data/lib/nfe/signer/signature.rb +113 -0
- data/lib/nfe/signer/version.rb +5 -0
- data/nfe-signer.gemspec +29 -0
- metadata +129 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 21bba212753355413acc9428620b8b72199f4c66
|
4
|
+
data.tar.gz: 693a6a2f356ffa8cd44f7a789ed1e06684e6b0dd
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ffd72e5417a2f38764fea7ab9845367fb6762e8c273cf8a5e62c758bcbd994d3543346a3e28dd7dd799583f77381b0849f951d94ccb3fd806f72035eaaf08300
|
7
|
+
data.tar.gz: f11f4574c912323b19cd58591ee4c25429bede75c6ec4fcf576534cbd5d96eef475cee3af06a0d2fbf5c049039b4a84822e32eb59f6e4f0031bcd5f99fd8429e
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
nfe
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.4
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
NFe Signer
|
2
|
+
==========
|
3
|
+
|
4
|
+
[](https://travis-ci.org/nfegeral/nfe-signer)
|
5
|
+
|
6
|
+
Assinador de Nota Fiscal Eletronica em ruby.
|
7
|
+
|
8
|
+
Versões suportadas
|
9
|
+
------------------
|
10
|
+
|
11
|
+
* Ruby 1.9.3
|
12
|
+
* Ruby 2.0.x
|
13
|
+
* Ruby 2.3.x
|
14
|
+
* Ruby 2.4.0
|
15
|
+
|
16
|
+
Install
|
17
|
+
-------
|
18
|
+
|
19
|
+
Se você estiver usando o `Bundler`, basta colocar essa linha no seu Gemfile:
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
gem 'nfe-signer'
|
23
|
+
```
|
24
|
+
|
25
|
+
Em seguida, execute o comando bundle install:
|
26
|
+
|
27
|
+
bundle
|
28
|
+
|
29
|
+
Caso contrário, você ainda pode executar um método de instalação padrão de gem:
|
30
|
+
|
31
|
+
gem install nfe-signer
|
32
|
+
|
33
|
+
Exemplo de Uso
|
34
|
+
--------------
|
35
|
+
|
36
|
+
Primeiro você deve obter a chave privada e o certificado:
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
key = OpenSSL::PKey::RSA.new(File.read("key.pem"))
|
40
|
+
cert = OpenSSL::X509::Certificate.new(File.read("cert.pem"))
|
41
|
+
```
|
42
|
+
|
43
|
+
Para carregar o xml a partir de um arquivo:
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
doc = File.open("Nfe.xml") { |f| Nfe::Signer(f) }
|
47
|
+
```
|
48
|
+
|
49
|
+
Para carregar o xml a partir de uma string:
|
50
|
+
|
51
|
+
```ruby
|
52
|
+
xml = %{
|
53
|
+
<NFe xmlns="http://www.portalfiscal.inf.br/nfe">
|
54
|
+
<infNFe Id='NFe35170101508376000174550010000000011000203062" versao="3.10">
|
55
|
+
...
|
56
|
+
</infNFe>
|
57
|
+
</NFe>
|
58
|
+
}
|
59
|
+
|
60
|
+
doc = Nfe::Signer(xml)
|
61
|
+
```
|
62
|
+
|
63
|
+
Para Assinar basta chamar o método sign! passando a chave privada e o certificado.
|
64
|
+
Para obter a string com o xml assinado basta executar o metódo to_xml.
|
65
|
+
|
66
|
+
```ruby
|
67
|
+
doc.sign!(key, cert)
|
68
|
+
doc.to_xml
|
69
|
+
```
|
70
|
+
|
71
|
+
## Contribuidores
|
72
|
+
|
73
|
+
* Ronaldo de Sousa Araujo ([ronaldoaraujo](https://github.com/ronaldoaraujo))
|
74
|
+
* Lucas Augusto Correa ([correalucas](https://github.com/correalucas))
|
75
|
+
|
76
|
+
## Copyright
|
77
|
+
|
78
|
+
Copyright (c) 2017 NFe Geral. Leia LICENSE para mais detalhes.
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "nfe/signer"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/lib/nfe/signer.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require "nokogiri"
|
2
|
+
require "nfe/signer/version"
|
3
|
+
require "nfe/signer/signature"
|
4
|
+
|
5
|
+
module Nfe
|
6
|
+
|
7
|
+
class << self
|
8
|
+
|
9
|
+
def Signer(thing)
|
10
|
+
Nokogiri::XML::Document.parse(thing, nil, "utf-8", Nokogiri::XML::ParseOptions::NOBLANKS)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
class Nokogiri::XML::Document
|
15
|
+
|
16
|
+
def sign!(cert, key)
|
17
|
+
Nfe::Signer::Signature.new(self, cert, key)
|
18
|
+
self
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,113 @@
|
|
1
|
+
require "base64"
|
2
|
+
|
3
|
+
module Nfe
|
4
|
+
module Signer
|
5
|
+
class Signature
|
6
|
+
attr_accessor :document, :key, :cert
|
7
|
+
|
8
|
+
def initialize(document, key, cert)
|
9
|
+
@document = document
|
10
|
+
@key = key
|
11
|
+
@cert = cert
|
12
|
+
document.root.add_child signature
|
13
|
+
end
|
14
|
+
|
15
|
+
def signature
|
16
|
+
# Create Signature
|
17
|
+
node = Nokogiri::XML::Node.new('Signature', @document)
|
18
|
+
node.default_namespace = 'http://www.w3.org/2000/09/xmldsig#'
|
19
|
+
node.add_child signature_info
|
20
|
+
node.add_child signature_value
|
21
|
+
node.add_child key_info
|
22
|
+
node
|
23
|
+
end
|
24
|
+
|
25
|
+
def signature_info
|
26
|
+
# Create SignatureInfo
|
27
|
+
signature_info_node = Nokogiri::XML::Node.new('SignedInfo', @document)
|
28
|
+
|
29
|
+
# Add CanonicalizationMethod
|
30
|
+
child_node = Nokogiri::XML::Node.new('CanonicalizationMethod', @document)
|
31
|
+
child_node['Algorithm'] = 'http://www.w3.org/TR/2001/REC-xml-c14n-20010315'
|
32
|
+
signature_info_node.add_child child_node
|
33
|
+
|
34
|
+
# Add SignatureMethod
|
35
|
+
child_node = Nokogiri::XML::Node.new('SignatureMethod', @document)
|
36
|
+
child_node['Algorithm'] = 'http://www.w3.org/2000/09/xmldsig#rsa-sha1'
|
37
|
+
signature_info_node.add_child child_node
|
38
|
+
|
39
|
+
# Add Reference
|
40
|
+
signature_info_node.add_child reference
|
41
|
+
signature_info_node
|
42
|
+
end
|
43
|
+
|
44
|
+
def signature_value
|
45
|
+
# Sign Signature
|
46
|
+
xml = Nokogiri::XML(signature_info.to_xml, &:noblanks)
|
47
|
+
xml.root["xmlns"] = 'http://www.w3.org/2000/09/xmldsig#'
|
48
|
+
sign_canon = xml.canonicalize(Nokogiri::XML::XML_C14N_EXCLUSIVE_1_0)
|
49
|
+
signature_hash = @key.sign(OpenSSL::Digest::SHA1.new, sign_canon)
|
50
|
+
|
51
|
+
# Add SignatureValue
|
52
|
+
node = Nokogiri::XML::Node.new('SignatureValue', @document)
|
53
|
+
node.content = Base64.encode64(signature_hash).gsub("\n", '')
|
54
|
+
node
|
55
|
+
end
|
56
|
+
|
57
|
+
def key_info
|
58
|
+
# Create KeyInfo
|
59
|
+
node = Nokogiri::XML::Node.new('KeyInfo', @document)
|
60
|
+
|
61
|
+
# Add X509 Data and Certificate
|
62
|
+
x509_data = Nokogiri::XML::Node.new('X509Data', @document)
|
63
|
+
x509_certificate = Nokogiri::XML::Node.new('X509Certificate', @document)
|
64
|
+
x509_certificate.content = @cert.to_pem.gsub(/\-\-\-\-\-[A-Z]+ CERTIFICATE\-\-\-\-\-/, "").gsub(/\n/,"")
|
65
|
+
|
66
|
+
x509_data.add_child x509_certificate
|
67
|
+
node.add_child x509_data
|
68
|
+
node
|
69
|
+
end
|
70
|
+
|
71
|
+
def reference
|
72
|
+
infNFe = @document.at_css("infNFe")
|
73
|
+
|
74
|
+
# Calculate digest
|
75
|
+
xml_canon = infNFe.canonicalize(Nokogiri::XML::XML_C14N_EXCLUSIVE_1_0)
|
76
|
+
xml_digest = Base64.encode64(OpenSSL::Digest::SHA1.digest(xml_canon)).strip
|
77
|
+
|
78
|
+
# Create Reference
|
79
|
+
node = Nokogiri::XML::Node.new('Reference', @document)
|
80
|
+
node['URI'] = "##{infNFe['Id']}"
|
81
|
+
node.add_child transforms
|
82
|
+
|
83
|
+
# Add Digest
|
84
|
+
child_node = Nokogiri::XML::Node.new('DigestMethod', @document)
|
85
|
+
child_node['Algorithm'] = 'http://www.w3.org/2000/09/xmldsig#sha1'
|
86
|
+
node.add_child child_node
|
87
|
+
|
88
|
+
# Add DigestValue
|
89
|
+
child_node = Nokogiri::XML::Node.new('DigestValue', @document)
|
90
|
+
child_node.content = xml_digest
|
91
|
+
node.add_child child_node
|
92
|
+
node
|
93
|
+
end
|
94
|
+
|
95
|
+
def transforms
|
96
|
+
# Add Transforms
|
97
|
+
node = Nokogiri::XML::Node.new('Transforms', @document)
|
98
|
+
|
99
|
+
# Add Transform
|
100
|
+
child_node = Nokogiri::XML::Node.new('Transform', @document)
|
101
|
+
child_node['Algorithm'] = 'http://www.w3.org/2000/09/xmldsig#enveloped-signature'
|
102
|
+
node.add_child child_node
|
103
|
+
|
104
|
+
# Add Transform
|
105
|
+
child_node = Nokogiri::XML::Node.new('Transform', @document)
|
106
|
+
child_node['Algorithm'] = 'http://www.w3.org/TR/2001/REC-xml-c14n-20010315'
|
107
|
+
node.add_child child_node
|
108
|
+
node
|
109
|
+
end
|
110
|
+
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
data/nfe-signer.gemspec
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'nfe/signer/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "nfe-signer"
|
8
|
+
spec.version = Nfe::Signer::VERSION
|
9
|
+
spec.authors = ["Ronaldo de Sousa Araujo"]
|
10
|
+
spec.email = ["contato@ronaldoaraujo.eti.br"]
|
11
|
+
|
12
|
+
spec.summary = "Assinador para Nota Fiscal Eletronica"
|
13
|
+
spec.description = "Gem criada para interagir com o sistema de notas fiscais eletronicas do governo brasileiro"
|
14
|
+
spec.homepage = "http://www.nfegeral.com.br"
|
15
|
+
spec.licenses = ["MIT"]
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
18
|
+
f.match(%r{^(test|spec|features)/})
|
19
|
+
end
|
20
|
+
spec.bindir = "exe"
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.13"
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
27
|
+
spec.add_development_dependency "nokogiri", "~> 1.7"
|
28
|
+
spec.add_development_dependency "openssl", "~> 2.0"
|
29
|
+
end
|
metadata
ADDED
@@ -0,0 +1,129 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: nfe-signer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ronaldo de Sousa Araujo
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-01-21 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.13'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.13'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: nokogiri
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.7'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.7'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: openssl
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '2.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '2.0'
|
83
|
+
description: Gem criada para interagir com o sistema de notas fiscais eletronicas
|
84
|
+
do governo brasileiro
|
85
|
+
email:
|
86
|
+
- contato@ronaldoaraujo.eti.br
|
87
|
+
executables: []
|
88
|
+
extensions: []
|
89
|
+
extra_rdoc_files: []
|
90
|
+
files:
|
91
|
+
- ".gitignore"
|
92
|
+
- ".rspec"
|
93
|
+
- ".ruby-gemset"
|
94
|
+
- ".ruby-version"
|
95
|
+
- ".travis.yml"
|
96
|
+
- Gemfile
|
97
|
+
- README.md
|
98
|
+
- Rakefile
|
99
|
+
- bin/console
|
100
|
+
- bin/setup
|
101
|
+
- lib/nfe/signer.rb
|
102
|
+
- lib/nfe/signer/signature.rb
|
103
|
+
- lib/nfe/signer/version.rb
|
104
|
+
- nfe-signer.gemspec
|
105
|
+
homepage: http://www.nfegeral.com.br
|
106
|
+
licenses:
|
107
|
+
- MIT
|
108
|
+
metadata: {}
|
109
|
+
post_install_message:
|
110
|
+
rdoc_options: []
|
111
|
+
require_paths:
|
112
|
+
- lib
|
113
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
119
|
+
requirements:
|
120
|
+
- - ">="
|
121
|
+
- !ruby/object:Gem::Version
|
122
|
+
version: '0'
|
123
|
+
requirements: []
|
124
|
+
rubyforge_project:
|
125
|
+
rubygems_version: 2.6.8
|
126
|
+
signing_key:
|
127
|
+
specification_version: 4
|
128
|
+
summary: Assinador para Nota Fiscal Eletronica
|
129
|
+
test_files: []
|