govdelivery-crypt 3.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7578fbf111ac783542fea3e327515383fd37ff2a
4
+ data.tar.gz: b317cb5e0e5803b4e5461707815b34824c81c836
5
+ SHA512:
6
+ metadata.gz: 60802e7a8bc5c2a00c414e9b261308769e7dcfd598e71001750e62cc4964fa7061295edc7328ad4d38054c67f3389bd3008d6c14a335488ba85baa6c62419049
7
+ data.tar.gz: 212241e298d5736b2329f99fb4d3b1bd87dc53b67fe32de42ae4478da4f2f89f8ff3e794532f7a80308deadd0aa67c62c8a2af289e2755b040e8762cf38de2bd
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in govdelivery-crypt.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
File without changes
data/README.md ADDED
@@ -0,0 +1,48 @@
1
+ # Govdelivery::Crypt
2
+
3
+ This is where we encrypt and decrypt things. Uses include:
4
+
5
+ * TMS recipient IDs
6
+ * CC bulletin recipient identifiers
7
+
8
+ There is also some Java code in BP2 that needs to be compatible with GovDelivery::Crypt::XSubscriber. Blacklist extends
9
+ this gem to support some old methods for encrypting subscriber IDs that we souldn't be using anymore
10
+
11
+ CI build tests on multiple rubies, the idea is that MRI and JRuby apps alike can use this.
12
+
13
+ ## Installation
14
+
15
+ Add this line to your application's Gemfile:
16
+
17
+ ```ruby
18
+ gem 'govdelivery-crypt', require: 'govdelivery/crypt'
19
+ ```
20
+
21
+ And then execute:
22
+
23
+ $ bundle
24
+
25
+ Or install it yourself as:
26
+
27
+ $ gem install govdelivery-crypt
28
+
29
+ Run the tests:
30
+
31
+ $ rake test
32
+
33
+ ## Usage
34
+
35
+
36
+ ## Development
37
+
38
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
39
+
40
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
41
+
42
+ ## Contributing
43
+
44
+ 1. Fork it ( https://github.com/[my-github-username]/govdelivery-crypt/fork )
45
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
46
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
47
+ 4. Push to the branch (`git push origin my-new-feature`)
48
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,24 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ # you also need to do this in your gemspec:
5
+ # if spec.respond_to?(:metadata)
6
+ # spec.metadata['allowed_push_host'] = "http://prod-rubygems1-ep.tops.gdi"
7
+ # end
8
+ module Bundler
9
+ class GemHelper
10
+ GEM_HOST = "http://prod-rubygems1-ep.tops.gdi"
11
+
12
+ protected
13
+ def rubygem_push(path)
14
+ sh("gem push '#{path}' --host #{GEM_HOST}")
15
+ Bundler.ui.confirm "Pushed #{name} #{version} to #{GEM_HOST}."
16
+ end
17
+ end
18
+ end
19
+
20
+ Rake::TestTask.new do |t|
21
+ t.libs << "test"
22
+ t.test_files = FileList['test/**/*_test.rb']
23
+ end
24
+
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "govdelivery/crypt"
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
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'govdelivery/crypt'
4
+ GovDelivery::Crypt::Cli.new(ARGV)
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'govdelivery/crypt/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "govdelivery-crypt"
8
+ spec.version = GovDelivery::Crypt::VERSION
9
+ spec.authors = ["Benjamin Ortega"]
10
+ spec.email = ["ben.ortega@gmail.com"]
11
+
12
+ if spec.respond_to?(:metadata)
13
+ spec.metadata['allowed_push_host'] = "http://prod-rubygems1-ep.tops.gdi"
14
+ end
15
+
16
+ spec.summary = %q{encryption wrappers for GovDelivery Ruby projects}
17
+ spec.description = %q{Encrypt and decrypt things to your heart's content.}
18
+ spec.homepage = "http://www.govdelivery.com"
19
+ spec.license = ""
20
+
21
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
22
+ spec.bindir = "exe"
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ["lib"]
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.7"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "minitest-spec-context"
29
+ spec.add_dependency 'activesupport'
30
+ spec.add_dependency 'multi_json'
31
+ end
@@ -0,0 +1,11 @@
1
+ require "govdelivery/crypt/version"
2
+ require 'openssl'
3
+ require 'base64'
4
+
5
+ require 'active_support/core_ext/object/blank'
6
+ require 'multi_json'
7
+ require 'govdelivery/crypt/cli'
8
+ require 'govdelivery/crypt/errors'
9
+ require 'govdelivery/crypt/open_ssl'
10
+ require 'govdelivery/crypt/x_subscriber'
11
+ require 'govdelivery/crypt/x_tms_recipient'
@@ -0,0 +1,36 @@
1
+ require 'optparse'
2
+
3
+ module GovDelivery
4
+ module Crypt
5
+ class Cli
6
+ def initialize(args)
7
+ options = OptionParser.new do |opts|
8
+ opts.banner = "Usage: govdelivery_crypt [options] (encrypted string)"
9
+
10
+ opts.on("-tCODE", "--tms-recipient=CODE", "Decrypt TMS recipient") do |encrypted_string|
11
+ puts GovDelivery::Crypt::XTmsRecipient.decrypt(encrypted_string)
12
+ exit
13
+ end
14
+
15
+ opts.on("-sCODE", "--subscriber=CODE", "Decrypt subscriber") do |encrypted_string|
16
+ puts GovDelivery::Crypt::XSubscriber.decrypt(encrypted_string)
17
+ exit
18
+ end
19
+
20
+ opts.on_tail("-h", "--help", "Show this message") do
21
+ puts opts
22
+ exit
23
+ end
24
+
25
+ # Another typical switch to print the version.
26
+ opts.on_tail("--version", "Show version") do
27
+ puts GovDelivery::Crypt::Version
28
+ exit
29
+ end
30
+ end.parse!(args)
31
+
32
+ puts "No options specified, see `govdelivery-crypt --help` for options"
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,6 @@
1
+ module GovDelivery
2
+ module Crypt
3
+ class Undecipherable < StandardError
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,27 @@
1
+ module GovDelivery
2
+ module Crypt
3
+ module OpenSSL
4
+ CIPHER = 'AES-256-ECB'
5
+
6
+ def to_crypt(string, key)
7
+ aes = get_aes(:encrypt, key)
8
+ Base64.strict_encode64("#{aes.update(string)+aes.final}")
9
+ end
10
+
11
+ def from_crypt(string, key)
12
+ aes = get_aes(:decrypt, key)
13
+ aes.update(Base64.strict_decode64(string))+aes.final
14
+ end
15
+
16
+
17
+ private
18
+
19
+ def get_aes(method, key)
20
+ aes = ::OpenSSL::Cipher::Cipher.new(CIPHER)
21
+ aes.send(method)
22
+ aes.key = key
23
+ aes
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,5 @@
1
+ module GovDelivery
2
+ module Crypt
3
+ VERSION = "3.1.0"
4
+ end
5
+ end
@@ -0,0 +1,111 @@
1
+ module GovDelivery
2
+ module Crypt
3
+ class Undecipherable < StandardError
4
+ end
5
+
6
+ class XSubscriber
7
+
8
+ # GD2 stored original email addresses in a header using a homebrew TEA implementation
9
+ # that doesn't appear to be compatible with other implementations. We shouldn't need this
10
+ # and it would only work in Java if we did include it, so including for posterity only.
11
+ class Version1
12
+
13
+ def encrypt(string)
14
+ raise NotImplementedError.new('X-Subscriber version 1 encryption is not implemented')
15
+ end
16
+
17
+ def decrypt(string)
18
+ raise NotImplementedError.new('X-Subscriber version 1 decryption is not implemented')
19
+ end
20
+
21
+ def decrypted_hash(string)
22
+ raise NotImplementedError.new('X-Subscriber version 1 decryption is not implemented')
23
+ end
24
+
25
+ end
26
+
27
+ # AES-256-ECB encryption of original email address
28
+ class Version2
29
+ include ::GovDelivery::Crypt::OpenSSL
30
+
31
+ KEY=Digest::SHA1.hexdigest('GovDelivery DCM x-subscriber secret key')[0..31]
32
+
33
+ def version
34
+ '2'
35
+ end
36
+
37
+ def encrypt(string)
38
+ "#{version}.#{to_crypt(string, KEY)}".strip
39
+ end
40
+
41
+ # returns a string representing the recipient's email address
42
+ def decrypt(string)
43
+ from_crypt(string, KEY)
44
+ end
45
+
46
+ # returns a subset of v3 payload data for forward compatibility with systems using v3 format
47
+ def decrypted_hash(string)
48
+ {'email' => from_crypt(string, KEY)}
49
+ end
50
+ end
51
+
52
+ # AES-256-ECB encryption of a JSON hash including
53
+ # email - original recipient email address
54
+ # src - original message sender and type
55
+ # user_id - ID of recipient in sending system
56
+ # mailing_id - ID of mailing in sending system
57
+ class Version3 < Version2
58
+
59
+ def version
60
+ '3'
61
+ end
62
+
63
+ # returns the payload JSON as a string
64
+ def decrypt(string)
65
+ from_crypt(string, KEY)
66
+ end
67
+
68
+ # returns the payload JSON transformed into a hash
69
+ def decrypted_hash(string)
70
+ MultiJson.load(from_crypt(string, KEY))
71
+ end
72
+ end
73
+
74
+ class << self
75
+
76
+
77
+ def encrypt(email, src=nil, mailing_id=nil, user_id=nil)
78
+ if src || mailing_id || user_id
79
+ Version3.new.encrypt(
80
+ MultiJson.dump(src: src,
81
+ email: email,
82
+ mailing_id: mailing_id,
83
+ user_id: user_id))
84
+ else
85
+ Version2.new.encrypt(email)
86
+ end
87
+ end
88
+
89
+ def decrypt(encrypted_address)
90
+ raise Undecipherable.new('X-subscriber header is missing or blank') if encrypted_address.blank?
91
+
92
+ parts = encrypted_address.split('.')
93
+ email_address = case parts.first
94
+ when '2'
95
+ Version2.new.decrypted_hash(parts[1].to_s)
96
+ when '3'
97
+ Version3.new.decrypted_hash(parts[1].to_s)
98
+ else
99
+ Version1.new.decrypted_hash(encrypted_address)
100
+ end
101
+
102
+ raise Undecipherable.new("X-Subscriber value #{encrypted_address} could not be parsed (got #{email_address})") unless email_address.is_a?(Hash)
103
+ email_address
104
+ end
105
+
106
+
107
+ end
108
+
109
+ end
110
+ end
111
+ end
@@ -0,0 +1,49 @@
1
+ module GovDelivery
2
+ module Crypt
3
+ class XTmsRecipient
4
+ # AES-256-ECB encryption of original email and TMS-XACT recipient ID
5
+ class Version1
6
+ include ::GovDelivery::Crypt::OpenSSL
7
+
8
+ DELIMITER = "::"
9
+ KEY = "e5004a37c17c9c94139fd7b2e82b61b1"
10
+
11
+ attr_accessor :email, :recipient_id
12
+
13
+ def initialize(email=nil, recipient_id=nil)
14
+ @email = email
15
+ @recipient_id = recipient_id
16
+ end
17
+
18
+ # email::recipient_id
19
+ def encrypt
20
+ string = [@email, @recipient_id].join('::')
21
+ "1.#{to_crypt(string, KEY)}".strip
22
+ end
23
+
24
+ # email::recipient_id
25
+ def decrypt(input)
26
+ @email, @recipient_id = from_crypt(input, KEY).split(DELIMITER)
27
+ self
28
+ end
29
+ end
30
+
31
+ class << self
32
+
33
+ def encrypt(*args)
34
+ Version1.new(*args).encrypt
35
+ end
36
+
37
+ def decrypt(payload)
38
+ raise Undecipherable.new('payload is missing or blank') if payload.blank?
39
+
40
+ parts = payload.split('.')
41
+ return Version1.new.decrypt(parts[1]) if parts.first=='1'
42
+ raise Undecipherable.new('payload is invalid')
43
+ end
44
+
45
+ end
46
+
47
+ end
48
+ end
49
+ end
metadata ADDED
@@ -0,0 +1,134 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: govdelivery-crypt
3
+ version: !ruby/object:Gem::Version
4
+ version: 3.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Benjamin Ortega
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-02-03 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.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
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: minitest-spec-context
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: activesupport
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: multi_json
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Encrypt and decrypt things to your heart's content.
84
+ email:
85
+ - ben.ortega@gmail.com
86
+ executables:
87
+ - govdelivery-crypt
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - .gitignore
92
+ - .travis.yml
93
+ - CODE_OF_CONDUCT.md
94
+ - Gemfile
95
+ - LICENSE.txt
96
+ - README.md
97
+ - Rakefile
98
+ - bin/console
99
+ - bin/setup
100
+ - exe/govdelivery-crypt
101
+ - govdelivery-crypt.gemspec
102
+ - lib/govdelivery/crypt.rb
103
+ - lib/govdelivery/crypt/cli.rb
104
+ - lib/govdelivery/crypt/errors.rb
105
+ - lib/govdelivery/crypt/open_ssl.rb
106
+ - lib/govdelivery/crypt/version.rb
107
+ - lib/govdelivery/crypt/x_subscriber.rb
108
+ - lib/govdelivery/crypt/x_tms_recipient.rb
109
+ homepage: http://www.govdelivery.com
110
+ licenses:
111
+ - ''
112
+ metadata:
113
+ allowed_push_host: http://prod-rubygems1-ep.tops.gdi
114
+ post_install_message:
115
+ rdoc_options: []
116
+ require_paths:
117
+ - lib
118
+ required_ruby_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - '>='
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ required_rubygems_version: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - '>='
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ requirements: []
129
+ rubyforge_project:
130
+ rubygems_version: 2.0.14.1
131
+ signing_key:
132
+ specification_version: 4
133
+ summary: encryption wrappers for GovDelivery Ruby projects
134
+ test_files: []