public function base32_decode($hash)
    {
        $lookup = $this->getLookup();

        if (preg_match('/^['.implode('', array_keys($lookup)).']+$/', $hash) == false) {
            throw new \InvalidArgumentException('Invalid base32 hash!');
        }

        $hash = strtoupper($hash);
        $buffer = 0;
        $length = 0;
        $binary = '';

        for ($i = 0; $i < strlen($hash); $i++) {
            $buffer = $buffer << 5;
            $buffer += $lookup[$hash[$i]];
            $length += 5;

            if ($length >= 8) {
                $length -= 8;
                $binary .= chr(($buffer & (0xFF << $length)) >> $length);
            }
        }

        return $binary;
    }