Class: SimpleHasher

Inherits:
Object
  • Object
show all
Defined in:
lib/simplehasher.rb,
lib/simplehasher/version.rb

Constant Summary collapse

ALLOWED_CHARS =
"CLK0oXklU2d6RvTrS1aDBx9GfN3e7FnQOtsmPi85MYq4AWHbZIuwJEgjVhpzyc"
VERSION =
"0.0.4"

Class Method Summary collapse

Class Method Details

.decode(hash) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/simplehasher.rb', line 15

def self.decode(hash)
  length = ALLOWED_CHARS.length
  size = hash.length - 1
  array = hash.split('')
  id = ALLOWED_CHARS.index(array.pop)
  i = 0
  array.each do |c| 
    id += ALLOWED_CHARS.index(c) * (length ** (size - i))
    i += 1
  end
  id
end

.encode(id) ⇒ Object



4
5
6
7
8
9
10
11
12
13
# File 'lib/simplehasher.rb', line 4

def self.encode(id)
  length = ALLOWED_CHARS.length

  while id > length -1
    hash = ALLOWED_CHARS[id % length,1].concat( hash || "" )
    id = (id / length).floor
  end

  ALLOWED_CHARS[id,1].concat(hash || "")
end