class String

Public Instance Methods

gen_phpass() click to toggle source
# 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) click to toggle source
# 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) click to toggle source

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

# 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) click to toggle source

Destructive version of String#rotate

# File lib/string/rotate.rb, line 9
def rotate!(count=1) self.replace(self.rotate(count)) end