cryptopro 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in cryptopro.gemspec
4
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
data/cryptopro.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "cryptopro/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "cryptopro"
7
+ s.version = Cryptopro::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["divineforest"]
10
+ # s.email = ["TODO: Write your email address"]
11
+ s.homepage = ""
12
+ s.summary = %q{CryptoPro ruby-wrapper for linux}
13
+ # s.description = %q{TODO: Write a gem description}
14
+
15
+ s.rubyforge_project = "cryptopro"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+
22
+ s.add_dependency "cocaine"
23
+ end
data/lib/cryptopro.rb ADDED
@@ -0,0 +1,4 @@
1
+ require 'cryptopro/signature'
2
+
3
+ module Cryptopro
4
+ end
@@ -0,0 +1,20 @@
1
+ Copyright 2011 YOURNAME
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,19 @@
1
+ h1. CryptoPro
2
+
3
+ ruby-wrapper for *nix CryptoPro CSP command line utility cryptcp
4
+
5
+ Ruby-обёртка для утилиты командной строки cryptcp от CryptoPro CSP в никсах.
6
+ Позволяет легко работать с ЭЦП из ruby.
7
+
8
+ h1. Использование
9
+
10
+ В Gemfile:
11
+
12
+ gem 'cryptopro'
13
+
14
+ В коде:
15
+
16
+ Cryptopro::Signature.verify(:message => "message", :signature => "signature", :certificate => "certificate")
17
+
18
+ Copyright (c) 2011 divineforest, evrone
19
+ This project rocks and uses MIT-LICENSE.
@@ -0,0 +1,82 @@
1
+ require 'tmpdir'
2
+ require 'digest/md5'
3
+ require 'cocaine'
4
+
5
+ module Cryptopro
6
+ class Signature
7
+ MESSAGE_FILE_NAME = "message.txt"
8
+ # Должен называться как файл с сообщением, только расширение .sgn
9
+ SIGNATURE_FILE_NAME = "message.txt.sgn"
10
+ CERTIFICATE_FILE_NAME = "certificate.cer"
11
+
12
+ CERTIFICATE_LINE_LENGTH = 64
13
+
14
+ # Options: message, signature, certificate
15
+ def self.verify(options)
16
+ raise "Message required" if (options[:message].nil? || options[:message] == "")
17
+ raise "Signature required" if (options[:signature].nil? || options[:signature] == "")
18
+ raise "Certificate required" if (options[:certificate].nil? || options[:certificate] == "")
19
+
20
+ tmp_dir = create_temp_dir
21
+ create_temp_files(tmp_dir, options)
22
+ valid = execute(tmp_dir)
23
+ end
24
+
25
+ private
26
+
27
+ # Для работы с cryptcp требуется, чтобы сообщение, полпись и сертификат были в виде файлов
28
+ # Создаётся временная уникальная папка для каждой проверки
29
+ def self.create_temp_dir
30
+ uniq_name = Digest::MD5.hexdigest("#{rand(1_000_000)}#{Time.now}")
31
+ full_name = "#{Dir.tmpdir}/cryptcp/#{uniq_name}"
32
+ FileUtils.mkdir_p(full_name)
33
+ end
34
+
35
+ def self.create_temp_files(tmp_dir, options)
36
+ # Создать файл сообщения
37
+ create_temp_file(tmp_dir, MESSAGE_FILE_NAME, options[:message])
38
+ # Создать файл подписи
39
+ create_temp_file(tmp_dir, SIGNATURE_FILE_NAME, options[:signature])
40
+ # Создать файл сертификата
41
+ certificate_with_container = add_container_to_certificate(options[:certificate])
42
+ create_temp_file(tmp_dir, CERTIFICATE_FILE_NAME, certificate_with_container)
43
+ end
44
+
45
+ def self.create_temp_file(dir_name, file_name, content)
46
+ File.open("#{dir_name}/#{file_name}", "w") { |file| file.write(content) }
47
+ end
48
+
49
+ # Обсуждение формата использования: http://www.cryptopro.ru/forum2/Default.aspx?g=posts&t=1516
50
+ # Пример вызова утилиты cryptcp:
51
+ # cryptcp -vsignf -dir /home/user/signs -f certificate.cer message.txt
52
+ # /home/user/signs -- папка с подписью, имя которой соответствуют имени сообщения, но с расширением .sgn
53
+ def self.execute(dir)
54
+ cmd = "cryptcp -vsignf -dir #{dir} -f #{dir}/#{CERTIFICATE_FILE_NAME} -nochain #{dir}/#{MESSAGE_FILE_NAME}"
55
+ line = Cocaine::CommandLine.new("cryptcp", "-vsignf -dir :signatures_dir -f :certificate -nochain :message",
56
+ :signatures_dir => dir,
57
+ :certificate => "#{dir}/#{CERTIFICATE_FILE_NAME}",
58
+ :message => "#{dir}/#{MESSAGE_FILE_NAME}"
59
+ )
60
+ begin
61
+ line.run
62
+ true
63
+ rescue Cocaine::ExitStatusError
64
+ false
65
+ rescue Cocaine::CommandNotFoundError => e
66
+ raise "Команда cryptcp не найдена"
67
+ end
68
+ end
69
+
70
+ # Добавляет -----BEGIN CERTIFICATE----- / -----END CERTIFICATE-----, если их нет.
71
+ # Так же делит длинную строку Base64 на строки по 64 символа.
72
+ # Это требование cryptcp к файл с сертификатом.
73
+ def self.add_container_to_certificate(certificate)
74
+ return certificate if certificate.downcase.include?("begin")
75
+
76
+ parts = certificate.scan(/.{1,#{CERTIFICATE_LINE_LENGTH}}/)
77
+ certificate_with_container = "-----BEGIN CERTIFICATE-----\n#{parts.join("\n")}\n-----END CERTIFICATE-----"
78
+ end
79
+
80
+ end
81
+
82
+ end
@@ -0,0 +1,3 @@
1
+ module Cryptopro
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cryptopro
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - divineforest
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-10-31 00:00:00 +03:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: cocaine
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ description:
36
+ email:
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files: []
42
+
43
+ files:
44
+ - .gitignore
45
+ - Gemfile
46
+ - Rakefile
47
+ - cryptopro.gemspec
48
+ - lib/cryptopro.rb
49
+ - lib/cryptopro/MIT-LICENSE
50
+ - lib/cryptopro/README.textile
51
+ - lib/cryptopro/signature.rb
52
+ - lib/cryptopro/version.rb
53
+ has_rdoc: true
54
+ homepage: ""
55
+ licenses: []
56
+
57
+ post_install_message:
58
+ rdoc_options: []
59
+
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ hash: 3
68
+ segments:
69
+ - 0
70
+ version: "0"
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ hash: 3
77
+ segments:
78
+ - 0
79
+ version: "0"
80
+ requirements: []
81
+
82
+ rubyforge_project: cryptopro
83
+ rubygems_version: 1.6.2
84
+ signing_key:
85
+ specification_version: 3
86
+ summary: CryptoPro ruby-wrapper for linux
87
+ test_files: []
88
+