human_digest 0.0.3

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
+ .rvmrc
2
+ .DS_Store
3
+ Gemfile.lock
4
+ human_digest-*.gem
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in human_hash.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ (The MIT License)
2
+
3
+ Copyright (c) 2012 Matt Vanderpol
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,19 @@
1
+ # HumanDigest
2
+
3
+ HumanDigest is a Ruby library that creates human-readable representations of digests.
4
+
5
+ ## Example
6
+
7
+ ```ruby
8
+ digest = '60ad8d0d871b6095808297'
9
+ puts HumanDigest.humanize(digest) # => sodium-magnesium-nineteen-hydrogen
10
+ ```
11
+
12
+ ## Inspiration
13
+
14
+ HumanDigest is a Ruby port of the [humanhash library for python](https://github.com/zacharyvoase/humanhash)
15
+
16
+
17
+ ## License
18
+
19
+ Copyright (c) 2012 Matt Vanderpol, released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require 'rspec/core/rake_task'
2
+
3
+ task :default => :spec
4
+
5
+ RSpec::Core::RakeTask.new do |t|
6
+ t.ruby_opts = ["-w"]
7
+ end
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "human_digest/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'human_digest'
7
+ s.version = HumanDigest::VERSION
8
+ s.date = '2012-02-29'
9
+ s.summary = "HumanDigest"
10
+ s.description = "Human-readable digests"
11
+ s.authors = ["Matt Vanderpol"]
12
+ s.email = 'matt@vanderpol.net'
13
+ s.files = ["lib/human_digest.rb"]
14
+ s.homepage = "https://github.com/bookwyrm/human_digest"
15
+
16
+ s.rubyforge_project = "human_digest"
17
+
18
+ s.files = `git ls-files`.split("\n")
19
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
20
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
21
+ s.require_paths = ["lib"]
22
+
23
+
24
+ s.add_development_dependency "rake"
25
+ s.add_development_dependency "rspec"
26
+ end
@@ -0,0 +1,3 @@
1
+ module HumanDigest
2
+ VERSION = "0.0.3"
3
+ end
@@ -0,0 +1,84 @@
1
+ require "human_digest/version"
2
+
3
+ # HumanDigest - Human-readable representations of digests
4
+ module HumanDigest
5
+ @word_list = [
6
+ 'ack', 'alabama', 'alanine', 'alaska', 'alpha', 'angel', 'apart', 'april',
7
+ 'arizona', 'arkansas', 'artist', 'asparagus', 'aspen', 'august', 'autumn',
8
+ 'avocado', 'bacon', 'bakerloo', 'batman', 'beer', 'berlin', 'beryllium',
9
+ 'black', 'blossom', 'blue', 'bluebird', 'bravo', 'bulldog', 'burger',
10
+ 'butter', 'california', 'carbon', 'cardinal', 'carolina', 'carpet', 'cat',
11
+ 'ceiling', 'charlie', 'chicken', 'coffee', 'cola', 'cold', 'colorado',
12
+ 'comet', 'connecticut', 'crazy', 'cup', 'dakota', 'december', 'delaware',
13
+ 'delta', 'diet', 'don', 'double', 'early', 'earth', 'east', 'echo',
14
+ 'edward', 'eight', 'eighteen', 'eleven', 'emma', 'enemy', 'equal',
15
+ 'failed', 'fanta', 'fifteen', 'fillet', 'finch', 'fish', 'five', 'fix',
16
+ 'floor', 'florida', 'football', 'four', 'fourteen', 'foxtrot', 'freddie',
17
+ 'friend', 'fruit', 'gee', 'georgia', 'glucose', 'golf', 'green', 'grey',
18
+ 'hamper', 'happy', 'harry', 'hawaii', 'helium', 'high', 'hot', 'hotel',
19
+ 'hydrogen', 'idaho', 'illinois', 'india', 'indigo', 'ink', 'iowa',
20
+ 'island', 'item', 'jersey', 'jig', 'johnny', 'juliet', 'july', 'jupiter',
21
+ 'kansas', 'kentucky', 'kilo', 'king', 'kitten', 'lactose', 'lake', 'lamp',
22
+ 'lemon', 'leopard', 'lima', 'lion', 'lithium', 'london', 'louisiana',
23
+ 'low', 'magazine', 'magnesium', 'maine', 'mango', 'march', 'mars',
24
+ 'maryland', 'massachusetts', 'may', 'mexico', 'michigan', 'mike',
25
+ 'minnesota', 'mirror', 'mississippi', 'missouri', 'mobile', 'mockingbird',
26
+ 'monkey', 'montana', 'moon', 'mountain', 'muppet', 'music', 'nebraska',
27
+ 'neptune', 'network', 'nevada', 'nine', 'nineteen', 'nitrogen', 'north',
28
+ 'november', 'nuts', 'october', 'ohio', 'oklahoma', 'one', 'orange',
29
+ 'oranges', 'oregon', 'oscar', 'oven', 'oxygen', 'papa', 'paris', 'pasta',
30
+ 'pennsylvania', 'pip', 'pizza', 'pluto', 'potato', 'princess', 'purple',
31
+ 'quebec', 'queen', 'quiet', 'red', 'river', 'robert', 'robin', 'romeo',
32
+ 'rugby', 'sad', 'salami', 'saturn', 'september', 'seven', 'seventeen',
33
+ 'shade', 'sierra', 'single', 'sink', 'six', 'sixteen', 'skylark', 'snake',
34
+ 'social', 'sodium', 'solar', 'south', 'spaghetti', 'speaker', 'spring',
35
+ 'stairway', 'steak', 'stream', 'summer', 'sweet', 'table', 'tango', 'ten',
36
+ 'tennessee', 'tennis', 'texas', 'thirteen', 'three', 'timing', 'triple',
37
+ 'twelve', 'twenty', 'two', 'uncle', 'undress', 'uniform', 'uranus', 'utah',
38
+ 'vegan', 'venus', 'vermont', 'victor', 'video', 'violet', 'virginia',
39
+ 'washington', 'west', 'whiskey', 'white', 'william', 'winner', 'winter',
40
+ 'wisconsin', 'wolfram', 'wyoming', 'xray', 'yankee', 'yellow', 'zebra',
41
+ 'zulu'
42
+ ]
43
+
44
+ # Make a digest human-readable
45
+ #
46
+ # Example:
47
+ # >> HumanDigest.humanize('60ad8d0d871b6095808297)
48
+ # => sodium-magnesium-nineteen-hydrogen
49
+ #
50
+ # Arguments:
51
+ # hexdigest: (String)
52
+ # words: (Integer)
53
+ # separator: (String)
54
+
55
+ def self.humanize(hexdigest, words=4, separator='-')
56
+ limit = hexdigest.length / 2
57
+ unpack_format = 'A2' * limit
58
+ bytes = hexdigest.unpack(unpack_format).collect { |x| x.hex }
59
+ compressed = compress(bytes, words)
60
+ return (compressed.collect { |x| @word_list[x] }).join(separator)
61
+ end
62
+
63
+ # Compress a byte array into a shorter length
64
+ #
65
+ # Example:
66
+ # >> HumanDigest.compress([96, 173, 141, 13, 135, 27, 96, 149, 128, 130, 151])
67
+ # => [205, 128, 156, 96]
68
+ #
69
+ # Arguments:
70
+ # bytes: (Array)
71
+ # target: (Integer)
72
+
73
+ def self.compress(bytes, target)
74
+ bytes_copy = bytes.clone
75
+ length = bytes_copy.length
76
+ seg_size = (length / target).floor
77
+ segments = []
78
+ target.times { segments.push(bytes_copy.slice!(0, seg_size)) }
79
+ segments[-1].concat(bytes_copy)
80
+ checksums = segments.collect { |a| a.reduce(:^) }
81
+ return checksums
82
+ end
83
+
84
+ end
@@ -0,0 +1,33 @@
1
+ require 'human_digest'
2
+
3
+ describe HumanDigest do
4
+ it "should generate a human digest" do
5
+ digest = '60ad8d0d871b6095808297'
6
+ HumanDigest.humanize(digest).should == 'sodium-magnesium-nineteen-hydrogen'
7
+ end
8
+
9
+ it "should compress byte arrays" do
10
+ byte_array = [96, 173, 141, 13, 135, 27, 96, 149, 128, 130, 151]
11
+ target = 4
12
+ HumanDigest.compress(byte_array, target).should == [205, 128, 156, 96]
13
+ end
14
+
15
+ it "should take a word count" do
16
+ digest = '60ad8d0d871b6095808297'
17
+ words = 4
18
+ HumanDigest.humanize(digest, words).should eq('sodium-magnesium-nineteen-hydrogen')
19
+
20
+ words = 3
21
+ HumanDigest.humanize(digest, words).should eq('equal-monkey-hydrogen')
22
+ end
23
+
24
+ it "should take a separator" do
25
+ digest = '60ad8d0d871b6095808297'
26
+ words = 4
27
+ separator = '-'
28
+ HumanDigest.humanize(digest, words, separator).should eq('sodium-magnesium-nineteen-hydrogen')
29
+
30
+ separator = ' '
31
+ HumanDigest.humanize(digest, words, separator).should eq('sodium magnesium nineteen hydrogen')
32
+ end
33
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: human_digest
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Matt Vanderpol
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-02-29 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: &70307781222500 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70307781222500
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec
27
+ requirement: &70307781222080 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70307781222080
36
+ description: Human-readable digests
37
+ email: matt@vanderpol.net
38
+ executables: []
39
+ extensions: []
40
+ extra_rdoc_files: []
41
+ files:
42
+ - .gitignore
43
+ - Gemfile
44
+ - LICENSE
45
+ - README.md
46
+ - Rakefile
47
+ - human_digest.gemspec
48
+ - lib/human_digest.rb
49
+ - lib/human_digest/version.rb
50
+ - spec/human_digest_spec.rb
51
+ homepage: https://github.com/bookwyrm/human_digest
52
+ licenses: []
53
+ post_install_message:
54
+ rdoc_options: []
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ requirements: []
70
+ rubyforge_project: human_digest
71
+ rubygems_version: 1.8.10
72
+ signing_key:
73
+ specification_version: 3
74
+ summary: HumanDigest
75
+ test_files:
76
+ - spec/human_digest_spec.rb