treehash 0.0.1

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.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/.travis.yml ADDED
@@ -0,0 +1,14 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - 1.9.3
5
+ - 1.8.7
6
+
7
+ branches:
8
+ only:
9
+ - master
10
+
11
+ notifications:
12
+ email:
13
+ recipients:
14
+ - treehash@erichmenge.com
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in treehash.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Erich Menge
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,16 @@
1
+ # Treehash [![Build Status](https://secure.travis-ci.org/erichmenge/treehash.png)](http://travis-ci.org/erichmenge/treehash)
2
+
3
+ Amazon Glacier uses a tree of SHA256 hashes. This is a tool to help you verify that your files were uploaded safely to
4
+ Glacier.
5
+
6
+ ### Installing it:
7
+
8
+ $ gem install treehash
9
+
10
+ ## Usage
11
+
12
+ ### Library:
13
+ Treehash::calculate_tree_hash string_or_io
14
+
15
+ ### Command-Line:
16
+ treehash filename
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new do |t|
5
+ t.pattern = "./spec/**/*_spec.rb"
6
+ end
7
+
8
+ task :default => :spec
data/bin/treehash ADDED
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'treehash'
4
+
5
+ ARGV.each do |file|
6
+ File.open file do |f|
7
+ puts Treehash::calculate_tree_hash f
8
+ end
9
+ end
data/lib/treehash.rb ADDED
@@ -0,0 +1,30 @@
1
+ require "treehash/version"
2
+ require 'stringio'
3
+ require 'digest'
4
+
5
+ module Treehash
6
+ MEGA_BYTE = 1024 * 1024
7
+
8
+ def self.calculate_tree_hash(string_or_io)
9
+ if string_or_io.is_a? String
10
+ handle = StringIO.new(string_or_io)
11
+ elsif string_or_io.is_a? IO
12
+ handle = string_or_io
13
+ else
14
+ raise "must be called with string or IO handle"
15
+ end
16
+
17
+ shas = []
18
+ while mega_byte = handle.read(MEGA_BYTE)
19
+ shas << Digest::SHA256.new.digest(mega_byte)
20
+ end
21
+
22
+ while shas.size > 1
23
+ shas = shas.each_slice(2).to_a.map do |pair|
24
+ pair[1] ? Digest::SHA256.new.update(pair[0]).update(pair[1]).digest : pair[0]
25
+ end
26
+ end
27
+
28
+ Digest.hexencode shas[0]
29
+ end
30
+ end
@@ -0,0 +1,3 @@
1
+ module Treehash
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1 @@
1
+ foo
@@ -0,0 +1,6 @@
1
+ require 'treehash'
2
+
3
+ RSpec.configure do |config|
4
+ config.treat_symbols_as_metadata_keys_with_true_values = true
5
+ config.order = 'random'
6
+ end
@@ -0,0 +1,26 @@
1
+ require 'spec_helper'
2
+
3
+ describe Treehash do
4
+ it { should respond_to(:calculate_tree_hash) }
5
+ specify { subject::MEGA_BYTE.should == 1024 * 1024 }
6
+
7
+ context "for large strings" do
8
+ it "should calculate the proper tree hash" do
9
+ hash = Treehash::calculate_tree_hash('boo' * 1048576)
10
+ hash.should == 'd53de7216088837ece12b81c0d233c18a2d0ef1f7e96b8f80cbf1b27f49dc4c5'
11
+ end
12
+ end
13
+
14
+ context "for small strings" do
15
+ it "should calculate the proper tree hash" do
16
+ hash = Treehash::calculate_tree_hash "foo"
17
+ hash.should == '2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae'
18
+ end
19
+
20
+ it "should calculate the proper tree hash when given a file" do
21
+ file = File.open File.join(File.dirname(__FILE__), 'fixtures', 'small_file')
22
+ hash = Treehash::calculate_tree_hash file
23
+ hash.should == '2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae'
24
+ end
25
+ end
26
+ end
data/treehash.gemspec ADDED
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'treehash/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "treehash"
8
+ gem.version = Treehash::VERSION
9
+ gem.authors = ["Erich Menge"]
10
+ gem.email = ["erich.menge@me.com"]
11
+ gem.description = %q{Calculates the SHA256 tree hash of a file or string. It also includes a command line tool.}
12
+ gem.summary = %q{Calculates the SHA256 tree hash of a file or string. It also includes a command line tool.}
13
+ gem.homepage = ""
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_development_dependency "rspec", "~> 2.0"
21
+ gem.add_development_dependency "rake", ">= 0.8.7"
22
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: treehash
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Erich Menge
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-04 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '2.0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '2.0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: 0.8.7
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: 0.8.7
46
+ description: Calculates the SHA256 tree hash of a file or string. It also includes
47
+ a command line tool.
48
+ email:
49
+ - erich.menge@me.com
50
+ executables:
51
+ - treehash
52
+ extensions: []
53
+ extra_rdoc_files: []
54
+ files:
55
+ - .gitignore
56
+ - .rspec
57
+ - .travis.yml
58
+ - Gemfile
59
+ - LICENSE.txt
60
+ - README.md
61
+ - Rakefile
62
+ - bin/treehash
63
+ - lib/treehash.rb
64
+ - lib/treehash/version.rb
65
+ - spec/fixtures/small_file
66
+ - spec/spec_helper.rb
67
+ - spec/tree_hash_spec.rb
68
+ - treehash.gemspec
69
+ homepage: ''
70
+ licenses: []
71
+ post_install_message:
72
+ rdoc_options: []
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ! '>='
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ segments:
82
+ - 0
83
+ hash: 614530273586430424
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ! '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ segments:
91
+ - 0
92
+ hash: 614530273586430424
93
+ requirements: []
94
+ rubyforge_project:
95
+ rubygems_version: 1.8.24
96
+ signing_key:
97
+ specification_version: 3
98
+ summary: Calculates the SHA256 tree hash of a file or string. It also includes a
99
+ command line tool.
100
+ test_files:
101
+ - spec/fixtures/small_file
102
+ - spec/spec_helper.rb
103
+ - spec/tree_hash_spec.rb