Class: WordHash
- Inherits:
-
Object
- Object
- WordHash
- Defined in:
- lib/word_hash.rb
Overview
Transforms hex digests to human-readable strings.
The format of these strings will look something like: ‘victor-bacon-zulu-lima`. The output is obtained by compressing the input digest to a fixed number of bytes, then mapping those bytes to one of 256 words. A default wordlist is provided, but you can override this if you prefer.
As long as you use the same wordlist, the output will be consistent (i.e. the same digest will always render the same representation).
Constant Summary collapse
- DEFAULT_WORDLIST =
%w( ack alabama alanine alaska alpha angel apart april arizona arkansas artist asparagus aspen august autumn avocado bacon bakerloo batman beer berlin beryllium black blossom blue bluebird bravo bulldog burger butter california carbon cardinal carolina carpet cat ceiling charlie chicken coffee cola cold colorado comet connecticut crazy cup dakota december delaware delta diet don double early earth east echo edward eight eighteen eleven emma enemy equal failed fanta fifteen fillet finch fish five fix floor florida football four fourteen foxtrot freddie friend fruit gee georgia glucose golf green grey hamper happy harry hawaii helium high hot hotel hydrogen idaho illinois india indigo ink iowa island item jersey jig johnny juliet july jupiter kansas kentucky kilo king kitten lactose lake lamp lemon leopard lima lion lithium london louisiana low magazine magnesium maine mango march mars maryland massachusetts may mexico michigan mike minnesota mirror mississippi missouri mobile mockingbird monkey montana moon mountain muppet music nebraska neptune network nevada nine nineteen nitrogen north november nuts october ohio oklahoma one orange oranges oregon oscar oven oxygen papa paris pasta pennsylvania pip pizza pluto potato princess purple quebec queen quiet red river robert robin romeo rugby sad salami saturn september seven seventeen shade sierra single sink six sixteen skylark snake social sodium solar south spaghetti speaker spring stairway steak stream summer sweet table tango ten tennessee tennis texas thirteen three timing triple twelve twenty two uncle undress uniform uranus utah vegan venus vermont victor video violet virginia washington west whiskey white william winner winter wisconsin wolfram wyoming xray yankee yellow zebra zulu )
Instance Attribute Summary collapse
-
#wordlist ⇒ Object
readonly
Returns the value of attribute wordlist.
Instance Method Summary collapse
-
#compress(bytes, target) ⇒ Object
Compress a list of byte values to a fixed target length.
-
#humanize(hexdigest, words = 4, separator = '-') ⇒ Object
Humanize a given hexadecimal digest.
-
#initialize(wordlist = DEFAULT_WORDLIST) ⇒ WordHash
constructor
A new instance of WordHash.
-
#uuid(*args) ⇒ Object
Generate a UUID with a human-readable representation.
Constructor Details
#initialize(wordlist = DEFAULT_WORDLIST) ⇒ WordHash
Returns a new instance of WordHash.
44 45 46 47 48 49 |
# File 'lib/word_hash.rb', line 44 def initialize(wordlist = DEFAULT_WORDLIST) fail ArgumentError, 'Wordlist must have exactly 256 items' unless wordlist.length == 256 @wordlist = wordlist end |
Instance Attribute Details
#wordlist ⇒ Object (readonly)
Returns the value of attribute wordlist.
14 15 16 |
# File 'lib/word_hash.rb', line 14 def wordlist @wordlist end |
Instance Method Details
#compress(bytes, target) ⇒ Object
Compress a list of byte values to a fixed target length.
Attempting to compress a smaller number of bytes to a larger number is an error
56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/word_hash.rb', line 56 def compress(bytes, target) fail ArgumentError, 'Fewer input bytes than requested output' if target > bytes.size slices = slice_bytes bytes, target # XOR checksum-like compression slices.map do |slice| slice.reduce :^ end end |
#humanize(hexdigest, words = 4, separator = '-') ⇒ Object
Humanize a given hexadecimal digest.
Change the number of words output by specifying ‘words`. Change the word separator with `separator`.
72 73 74 75 76 77 78 79 80 |
# File 'lib/word_hash.rb', line 72 def humanize(hexdigest, words = 4, separator = '-') bytes = hexdigest.split('') .each_slice(2).to_a bytes.pop if bytes.last.size == 1 bytes = bytes.map { |byte| byte.join('').to_i 16 } wordify_bytes(bytes, words, separator) end |
#uuid(*args) ⇒ Object
Generate a UUID with a human-readable representation.
Returns ‘[human_repr, full_digest]`. Accepts the same keyword arguments as `humanize`
86 87 88 89 90 |
# File 'lib/word_hash.rb', line 86 def uuid(*args) digest = SecureRandom.uuid.delete '-' word_hash = humanize digest, *args [digest, word_hash] end |