Class: String

Inherits:
Object show all
Defined in:
lib/string/phpass.rb,
lib/string/rotate.rb

Instance Method Summary collapse

Instance Method Details

#gen_phpassObject



39
40
41
42
# File 'lib/string/phpass.rb', line 39

def gen_phpass
	# todo: better random generation?
	self.phpass('$P$B' + ([*'0'..'9'] + [*'a'..'z'] + [*'A'..'Z']).sample(8).join)
end

#phpass(encpass) ⇒ Object



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
# File 'lib/string/phpass.rb', line 4

def phpass(encpass)
	return "*" if !encpass || encpass.size < 12
	table = './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
	cnt = table.index(encpass[3..3])
	return "*" if cnt > 31
	pass = self
	cnt = 1 << cnt
	salt = encpass[4, 8]
	hash = Digest::MD5.digest(salt + pass)
	cnt.times{
		hash = Digest::MD5.digest(hash + pass)
	}

	result = encpass[0, 12]
	i = 0
	cnt = 16
	while i < cnt
		val = hash[i].ord; i += 1
		result += table[val & 0x3f, 1]

		val |= hash[i].ord << 8 if i < cnt
		result += table[(val >> 6) & 0x3f, 1]
		i += 1
		break if i >= cnt

		val |= hash[i].ord << 16 if i < cnt
		result += table[(val >> 12) & 0x3f, 1]
		i += 1
		break if i >= cnt

		result += table[(val >> 18) & 0x3f, 1]
	end
	return result
end

#rotate(count = 1) ⇒ Object

Rotate string to the left with count. Specifying negative number indicates rotation to the right.



4
5
6
7
# File 'lib/string/rotate.rb', line 4

def rotate(count=1)
	count+=self.length if count<0
	self.slice(count,self.length-count)+self.slice(0,count)
end

#rotate!(count = 1) ⇒ Object

Destructive version of String#rotate



9
# File 'lib/string/rotate.rb', line 9

def rotate!(count=1) self.replace(self.rotate(count)) end