humanhash 0.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: 28d8ec5e6e2033ba568335b1c13cf78faa790df0
4
+ data.tar.gz: 05d609e3a0d25494f7e1d26a1648d5816ff0514c
5
+ SHA512:
6
+ metadata.gz: e37e624b96dff6f8f0f0c8a117c4664515f3c73bffdf4b534b029ccc2ddfa6074e6e72c1e890a3380fa74c6a27b205326d929fe5b3d55da543fdb4751e2b18ca
7
+ data.tar.gz: 6febea86a4ac16e06218b062d83571ef9a04d7003cffbfdb7bafc8df7a59cf495ae02702e469b7c0be65ffd56f29930254bb28d04413965a9f1e78d4a2221469
data/README.md ADDED
@@ -0,0 +1,63 @@
1
+ #humanhash-ruby
2
+
3
+ humanhash provides human-readable representations of digests.
4
+
5
+ This is a fork of Jacob Carlson's [port](https://github.com/jacobwcarlson/humanhash-ruby)
6
+ of a HumanHash implementation. It rearranges the library, adding some tests
7
+ and helper methods to make usage more similar to [Zachary Voase's Python
8
+ implementation](https://github.com/zacharyvoase/humanhash), while retaining extensibility.
9
+
10
+ ## Examples
11
+
12
+ irb(main):001:0> require 'humanhash'
13
+ irb(main):002:0> digest = '535061bddb0549f691c8b9c012a55ee2'
14
+ irb(main):003:0> HumanHash.humanize(digest)
15
+ => "alpha-twenty-mockingbird-twelve"
16
+ irb(main):004:0>
17
+ irb(main):005:0* HumanHash.uuid
18
+ => ["illinois-michigan-sad-bulldog", "7030c321-c015-4f84-88df-8da92138831b"]
19
+
20
+ ## Caveats
21
+
22
+ Don't store the humanhash output, as its statistical uniqueness is only
23
+ 256^words. Its intended use is as a human-readable (and, most
24
+ importantly, **memorable**) representation of a longer digest, unique enough
25
+ for display in a user interface, where a user may need to remember or verbally
26
+ communicate the identity of a hash, without having to remember a 40-character
27
+ hexadecimal sequence. Nevertheless, you should keep original digests around,
28
+ then pass them through `humanize()` only as you're displaying them.
29
+
30
+
31
+ ## How It Works
32
+
33
+ The procedure for generating a humanhash involves compressing the input to a
34
+ fixed length (default: 4 bytes), then mapping each of these bytes to a word in
35
+ a pre-defined wordlist (a default wordlist is supplied with the library). This
36
+ algorithm is consistent, so the same input, given the same wordlist, will
37
+ always give the same output. You can also use your own wordlist, and specify a
38
+ different number of words for output.
39
+
40
+
41
+ ## (Un)license
42
+
43
+ This is free and unencumbered software released into the public domain.
44
+
45
+ Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
46
+ software, either in source code form or as a compiled binary, for any purpose,
47
+ commercial or non-commercial, and by any means.
48
+
49
+ In jurisdictions that recognize copyright laws, the author or authors of this
50
+ software dedicate any and all copyright interest in the software to the public
51
+ domain. We make this dedication for the benefit of the public at large and to
52
+ the detriment of our heirs and successors. We intend this dedication to be an
53
+ overt act of relinquishment in perpetuity of all present and future rights to
54
+ this software under copyright law.
55
+
56
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
57
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
58
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE
59
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
60
+ CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
61
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
62
+
63
+ For more information, please refer to <http://unlicense.org/>
data/lib/humanhash.rb ADDED
@@ -0,0 +1,48 @@
1
+ require 'securerandom'
2
+ require 'humanhash/version'
3
+ require 'humanhash/wordlist'
4
+
5
+ module HumanHash
6
+
7
+ class HumanHasher
8
+ attr_accessor :words
9
+ attr_accessor :separator
10
+ attr_accessor :wordlist
11
+
12
+ def initialize(opts={})
13
+ @words = opts.fetch(:words, 4)
14
+ @separator = opts.fetch(:separator, '-')
15
+ @wordlist = opts.fetch(:wordlist, DEFAULT_WORDLIST)
16
+ nil
17
+ end
18
+
19
+ def humanize(hexdigest)
20
+ bytes = [hexdigest].pack("H*").bytes.to_a
21
+ compress(bytes, words).map{|x| wordlist[x]}.join(separator)
22
+ end
23
+
24
+ def compress(bytes, target)
25
+ len = bytes.size
26
+ return bytes unless target < len
27
+
28
+ segment_size = (len / target) + 1;
29
+ bytes.each_slice(segment_size).map{|x| x.reduce{|s,b| s ^ b}}
30
+ end
31
+
32
+ def uuid
33
+ uuid = SecureRandom.uuid.gsub('-', '')
34
+ [ humanize(uuid), uuid ]
35
+ end
36
+
37
+ end
38
+
39
+ def self.humanize(hexdigest, opts={})
40
+ h = HumanHasher.new(opts)
41
+ h.humanize(hexdigest)
42
+ end
43
+
44
+ def self.uuid(opts={})
45
+ h = HumanHasher.new(opts)
46
+ h.uuid
47
+ end
48
+ end
@@ -0,0 +1,3 @@
1
+ module HumanHash
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,40 @@
1
+ module HumanHash
2
+ DEFAULT_WORDLIST = [
3
+ 'ack', 'alabama', 'alanine', 'alaska', 'alpha', 'angel', 'apart', 'april',
4
+ 'arizona', 'arkansas', 'artist', 'asparagus', 'aspen', 'august', 'autumn',
5
+ 'avocado', 'bacon', 'bakerloo', 'batman', 'beer', 'berlin', 'beryllium',
6
+ 'black', 'blossom', 'blue', 'bluebird', 'bravo', 'bulldog', 'burger',
7
+ 'butter', 'california', 'carbon', 'cardinal', 'carolina', 'carpet', 'cat',
8
+ 'ceiling', 'charlie', 'chicken', 'coffee', 'cola', 'cold', 'colorado',
9
+ 'comet', 'connecticut', 'crazy', 'cup', 'dakota', 'december', 'delaware',
10
+ 'delta', 'diet', 'don', 'double', 'early', 'earth', 'east', 'echo',
11
+ 'edward', 'eight', 'eighteen', 'eleven', 'emma', 'enemy', 'equal',
12
+ 'failed', 'fanta', 'fifteen', 'fillet', 'finch', 'fish', 'five', 'fix',
13
+ 'floor', 'florida', 'football', 'four', 'fourteen', 'foxtrot', 'freddie',
14
+ 'friend', 'fruit', 'gee', 'georgia', 'glucose', 'golf', 'green', 'grey',
15
+ 'hamper', 'happy', 'harry', 'hawaii', 'helium', 'high', 'hot', 'hotel',
16
+ 'hydrogen', 'idaho', 'illinois', 'india', 'indigo', 'ink', 'iowa',
17
+ 'island', 'item', 'jersey', 'jig', 'johnny', 'juliet', 'july', 'jupiter',
18
+ 'kansas', 'kentucky', 'kilo', 'king', 'kitten', 'lactose', 'lake', 'lamp',
19
+ 'lemon', 'leopard', 'lima', 'lion', 'lithium', 'london', 'louisiana',
20
+ 'low', 'magazine', 'magnesium', 'maine', 'mango', 'march', 'mars',
21
+ 'maryland', 'massachusetts', 'may', 'mexico', 'michigan', 'mike',
22
+ 'minnesota', 'mirror', 'mississippi', 'missouri', 'mobile', 'mockingbird',
23
+ 'monkey', 'montana', 'moon', 'mountain', 'muppet', 'music', 'nebraska',
24
+ 'neptune', 'network', 'nevada', 'nine', 'nineteen', 'nitrogen', 'north',
25
+ 'november', 'nuts', 'october', 'ohio', 'oklahoma', 'one', 'orange',
26
+ 'oranges', 'oregon', 'oscar', 'oven', 'oxygen', 'papa', 'paris', 'pasta',
27
+ 'pennsylvania', 'pip', 'pizza', 'pluto', 'potato', 'princess', 'purple',
28
+ 'quebec', 'queen', 'quiet', 'red', 'river', 'robert', 'robin', 'romeo',
29
+ 'rugby', 'sad', 'salami', 'saturn', 'september', 'seven', 'seventeen',
30
+ 'shade', 'sierra', 'single', 'sink', 'six', 'sixteen', 'skylark', 'snake',
31
+ 'social', 'sodium', 'solar', 'south', 'spaghetti', 'speaker', 'spring',
32
+ 'stairway', 'steak', 'stream', 'summer', 'sweet', 'table', 'tango', 'ten',
33
+ 'tennessee', 'tennis', 'texas', 'thirteen', 'three', 'timing', 'triple',
34
+ 'twelve', 'twenty', 'two', 'uncle', 'undress', 'uniform', 'uranus', 'utah',
35
+ 'vegan', 'venus', 'vermont', 'victor', 'video', 'violet', 'virginia',
36
+ 'washington', 'west', 'whiskey', 'white', 'william', 'winner', 'winter',
37
+ 'wisconsin', 'wolfram', 'wyoming', 'xray', 'yankee', 'yellow', 'zebra',
38
+ 'zulu'
39
+ ]
40
+ end
metadata ADDED
@@ -0,0 +1,50 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: humanhash
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jacob Carlson
8
+ - Ross Duggan
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-06-02 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: humanhash provides human-readable representations of digests.
15
+ email:
16
+ - ross.duggan@acm.org
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/humanhash/version.rb
22
+ - lib/humanhash/wordlist.rb
23
+ - lib/humanhash.rb
24
+ - README.md
25
+ homepage: https://github.com/duggan/humanhash-ruby
26
+ licenses:
27
+ - Unlicense
28
+ metadata: {}
29
+ post_install_message:
30
+ rdoc_options: []
31
+ require_paths:
32
+ - lib
33
+ required_ruby_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ required_rubygems_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ requirements: []
44
+ rubyforge_project:
45
+ rubygems_version: 2.0.14
46
+ signing_key:
47
+ specification_version: 4
48
+ summary: Human-readable digests.
49
+ test_files: []
50
+ has_rdoc: