def build(self, stamp, body, passwd=""): target = body.keys() bodyary = [] for key in target: bodyary.append(key + ":" + body[key]) self[key] = body[key] bodystr = "<>".join(bodyary) if passwd != "": (pubkey, prikey) = apollo.key_pair(passwd) md = md5.new(bodystr).hexdigest() sign = apollo.sign(md, pubkey, prikey) self["pubkey"] = pubkey self["sign"] = sign self["target"] = ",".join(target) bodystr += "<>pubkey:" + pubkey + \ "<>sign:" + sign + \ "<>target:" + ",".join(target) id = md5.new(bodystr).hexdigest() self.stamp = int(stamp) self.recstr = str(stamp) + "<>" + id + "<>" + bodystr self.idstr = str(stamp) + "_" + id self["stamp"] = str(stamp) self["id"] = id self.id = id self.setpath() return id
def make512pq(keystr): seedbuf = md5.new(keystr).digest() seedbuf += md5.new(keystr+"pad1").digest() seedbuf += md5.new(keystr+"pad2").digest() seedbuf += md5.new(keystr+"pad3").digest() p = bin_to_int(seedbuf[0:28]) q = bin_to_int(seedbuf[28:64]) p |= 2**215 q |= 2**279 return (p, q)
def make512pq(keystr): seedbuf = md5.new(keystr).digest() seedbuf += md5.new(keystr + "pad1").digest() seedbuf += md5.new(keystr + "pad2").digest() seedbuf += md5.new(keystr + "pad3").digest() p = bin_to_int(seedbuf[0:28]) q = bin_to_int(seedbuf[28:64]) p |= 2**215 q |= 2**279 return (p, q)
def print_thread(self, path, id='', page=0): str_path = self.str_encode(path) file_path = self.file_encode('thread', path) form = cgi.FieldStorage(environ=self.environ, fp=self.stdin) self.archive_uri = '%s%s/' % (config.archive_uri, md5.new(file_path).hexdigest()) cache = Cache(file_path) if id and form.getfirst('ajax'): self.print_thread_ajax(path, id, form) return if cache.has_record(): pass elif self.check_get_cache(): if not form.getfirst('search_new_file', ''): cache.standby_directories() self.unlock() else: self.get_cache(cache) else: self.print404(id=id) return if config.use_cookie and len(cache) and (not id) and (not page): access = None try: cookie = SimpleCookie(self.environ.get('HTTP_COOKIE', '')) if 'access' in cookie: access = cookie['access'].value except CookieError, err: self.stderr.write('%s\n' % err) newcookie = self.setcookie(cache, access)
def make_sid(self): """Make admin sid for security.""" sidfile = config.admin_sid r = "" for i in range(4): r += str(random()) sid = md5.new(r).hexdigest() try: file(sidfile, "wb").write(sid + "\n") except IOError, err: self.stderr.write("%s: IOError: %s\n" % (sidfile, err))
def print_thread(self, path, id='', page=0): str_path = self.str_encode(path) file_path = self.file_encode('thread', path) self.archive_uri = '%s%s/' % (config.archive_uri, md5.new(file_path).hexdigest()) cache = Cache(file_path) if cache.has_record(): pass elif self.check_get_cache(): self.get_cache(cache) else: self.print404(id=id) return ids = cache.keys() self.header(path) form = self.form self.stdout.write('<p id="pagenavi">\n') self.stdout.write('<a href="%s" accesskey="0">[0]%s</a> | ' % (self.mobile_cgi, self.message['top'])) self.stdout.write( ' <a href="#bottom" id="top" accesskey="8">[8]%s</a>\n' % self.message['bottom_of_page']) self.print_page_navi(page, cache, path, str_path, id) self.stdout.write('</p>\n<div id="records">\n') page_size = config.mobile_page_size if id: inrange = ids elif page: inrange = ids[-page_size*(page+1):-page_size*page] else: inrange = ids[-page_size*(page+1):] printed = False for k in inrange: rec = cache[k] if ((not id) or (rec.id[:8] == id)) and rec.load_body(): self.print_record(cache, rec, path, str_path, id) printed = True rec.free() self.stdout.write("</div>\n") if id and (not printed) and config.archive_uri: self.print_jump('%s%s.html' % (self.archive_uri, id)) escaped_path = cgi.escape(path) escaped_path = re.sub(r' ', ' ', escaped_path) var = { 'cache': cache, } self.stdout.write(self.template('mobile_thread_top', var)) if len(cache): self.print_page_navi(page, cache, path, str_path, id) self.stdout.write('</p>\n') self.print_post_form(cache, id=id) self.remove_file_form(cache, escaped_path) self.footer()
def print_thread(self, path, id='', page=0): str_path = self.str_encode(path) file_path = self.file_encode('thread', path) self.archive_uri = '%s%s/' % (config.archive_uri, md5.new(file_path).hexdigest()) cache = Cache(file_path) if cache.has_record(): pass elif self.check_get_cache(): self.get_cache(cache) else: self.print404(id=id) return ids = cache.keys() self.header(path) form = self.form self.stdout.write('<p id="pagenavi">\n') self.stdout.write('<a href="%s" accesskey="0">[0]%s</a> | ' % (self.mobile_cgi, self.message['top'])) self.stdout.write( ' <a href="#bottom" id="top" accesskey="8">[8]%s</a>\n' % self.message['bottom_of_page']) self.print_page_navi(page, cache, path, str_path, id) self.stdout.write('</p>\n<div id="records">\n') page_size = config.mobile_page_size if id: inrange = ids elif page: inrange = ids[-page_size * (page + 1):-page_size * page] else: inrange = ids[-page_size * (page + 1):] printed = False for k in inrange: rec = cache[k] if ((not id) or (rec.id[:8] == id)) and rec.load_body(): self.print_record(cache, rec, path, str_path, id) printed = True rec.free() self.stdout.write("</div>\n") if id and (not printed) and config.archive_uri: self.print_jump('%s%s.html' % (self.archive_uri, id)) escaped_path = cgi.escape(path) escaped_path = re.sub(r' ', ' ', escaped_path) var = { 'cache': cache, } self.stdout.write(self.template('mobile_thread_top', var)) if len(cache): self.print_page_navi(page, cache, path, str_path, id) self.stdout.write('</p>\n') self.print_post_form(cache, id=id) self.remove_file_form(cache, escaped_path) self.footer()
def update(self): now = int(time()) tmp = [] changed = False for addr, stamp in self.available: if now < stamp + config.tmpaddr_span: tmp.append((addr, stamp)) else: changed = True if len(tmp) < config.tmpaddr_size: r = '' for i in range(4): r += str(random()) addr = md5.new(r).hexdigest()[:config.tmpaddr_length] tmp.append((addr, now)) changed = True if changed: self.available = tmp self.sync()
def check_sign(self): """Check sigunature.""" for k in ("pubkey", "sign", "target"): if k not in self: return False target = "" for t in self["target"].split(","): try: target += "<>" + t + ":" + self[t] except KeyError: return False except ValueError: return False target = target[2:] # remove ^<> md = md5.new(target).hexdigest() if apollo.verify(md, self["sign"], self["pubkey"]): return True else: return False
def md5check(self): buf = str(self).split("<>", 2) if (len(buf) > 2): return md5.new(buf[2]).hexdigest() == self["id"] else: return False
def cut_key(key): """Cut KeyStr to 11words.""" short_key = base64.encodestring(md5.new(key[:512]).digest())[:11] return short_key