Class: HashId

Inherits:
Object
  • Object
show all
Defined in:
lib/hashid.rb

Defined Under Namespace

Classes: Error

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(salt = nil) ⇒ HashId



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/hashid.rb', line 4

def initialize(salt = nil)
	@salt = salt || 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'
	
	@letters = [
		'P0MVLWNTZD',
		'UE58X2VSCT',
		'43Z0Y9AQLS',
		'V5Q1P4E8L9',
		'MOIRZCWXED',
		'7NBYXCZU9R',
		'G2S90I84MC',
		'TQAMIWH4ZC',
		'POERHBYA1F',
		'XKO20UB3IL',
		'Y0NTH78VSO',
		'P4DE6VUZLK',
		'9OKN2JLQGI',
		'61UTFSN0BR',
		'R7ZAHJVGE9',
		'ADHIZ5WNUQ',
		'Z9SPX2OQI5',
		'V7YFEU4PTJ',
		'YZVFD6UXNG',
		'VH8UDLCE93',
		'W0H5GMTFZK',
		'GXE6CT2VOP',
		'QH2FLZOM9V',
		'MBGHIF78PU',
		'MHS04FY8L7',
		'E95IGRSMZ1',
		'MN8AILRB95',
		'S7JVO4EHCD',
		'GT01ZPLVUA',
		'9OKN2JLQGI',
		'DUF24AGTSN',
		'4ASPNFJ2BR',
		'OR465YEXQV',
		'DUF24AGTSN',
		'M78PCRAGKS',
		'D8LVQSKA7U'
	]
	
	@indexes = [
		[0,0],
		[0,1],
		[1,0],
		[2,0],
		[0,2],
		[1,1],
		[2,1],
		[1,2]
	]
end

Class Method Details

.generate_saltObject



58
59
60
# File 'lib/hashid.rb', line 58

def self.generate_salt
	'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'.split(//).shuffle.join('')
end

Instance Method Details

#decrypt(string) ⇒ Object

Raises:



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/hashid.rb', line 97

def decrypt(string)

	# Require a string for the decryption process
	raise Error, 'HashID.decrypt expected a String as its argument' unless string.is_a?(String)

	# Prep the string for decryption
	string = string.upcase.gsub(/[^A-Z0-9]+/, '').split(//)
	
	# Require no less then 8 characters
	raise Error, 'A minimum of 8 characters is required for decryption' if string.length < 8
	
	begin
		# Get the indexes and index id
		indexid = @salt.index(string.pop)
		injections = @indexes[indexid]
		
		# Go ahead and locate the salts
		saltids = [string.pop]
		saltids << string.slice!(string.length - 1 - injections.last)
		saltids << string.slice!(injections.first)
		
		# Go ahead and reverse and determine the salt ids
		saltids = saltids.reverse.collect{|x|@salt.index(x)}
		
		# Get the letters cooresponding to the salts
		salts = saltids.collect{|x|@letters[x]}
		
		# Go ahead and convert the alphanumeric id back into numbers
		i=2;string=string.collect{|x|i==2?i=0:i+=1;salts[i].index(x)}.join('')
		
		# Return the integer
		return string.to_i
	rescue
		raise Error, 'The ID provided was not decryptable'
	end
end

#encrypt(string, len = 8) ⇒ Object

Raises:



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/hashid.rb', line 62

def encrypt(string, len = 8)

	# Require a fixnum for the encryption process
	raise Error, 'HashID.encrypt expected a Fixnum as its argument' unless string.is_a?(Fixnum)
	
	# Require a fixnum for the encryption process
	raise Error, 'HashID.encrypt must have a length of no less then 6' unless string.is_a?(Fixnum)

	# Prep the string for encryption
	string = string.to_s.rjust(len, '0')

	# Get the salt ids to use
	saltids = Array.new
	random = [*0..35].shuffle
	3.times{saltids<<random.shift}
	
	# Get the letters cooresponding to the salts
	salts = saltids.collect{|x|@letters[x]}
	
	# Go ahead and convert the numbers into the new format
	i=2;string=string.split(//).collect{|x|i==2?i=0:i+=1;salts[i][x.to_i,1]}.join('')
	
	# Turn the salt ids into their letter forms
	saltids.collect!{|x|@salt[x,1]}
	
	# Get the index id and the indexes
	indexid = [*0..7].shuffle.first
	injections = @indexes[indexid]
	
	# Inject the indexes and index id
	string.insert(injections.first, saltids[0])
	string.insert(string.length - injections.last, saltids[1])
	string << saltids[2] + @salt[indexid,1]
end