def get_auth_response(self, http_method, fullpath, username, nonce, realm, qop, cnonce, nc): """ Returns the server-computed digest response key. http_method: The request method, e.g. GET username: The user to be authenticated fullpath: The absolute URI to be accessed by the user nonce: A server-specified data string which should be uniquely generated each time a 401 response is made realm: A string to be displayed to users so they know which username and password to use qop: Indicates the "quality of protection" values supported by the server. The value "auth" indicates authentication. cnonce: An opaque quoted string value provided by the client and used by both client and server to avoid chosen plaintext attacks, to provide mutual authentication, and to provide some message integrity protection. nc: Hexadecimal request counter """ ha1 = self.authfunc(realm, username) ha2 = md5.md5('%s:%s' % (http_method, fullpath)).hexdigest() if qop: chk = "%s:%s:%s:%s:%s:%s" % (ha1, nonce, nc, cnonce, qop, ha2) else: chk = "%s:%s:%s" % (ha1, nonce, ha2) computed_response = md5.md5(chk).hexdigest() return computed_response
def build_conf(self): # Create conf file with peers for every other intercoonected server servers = self.search([('is_interconnected', '=', True)]) protocol = self.env['res.config.settings']._get_asterisk_param( 'interconnection_protocol') if not servers or len(servers) == 1: # Remove all server peers conf conf = self.env['asterisk.conf'].search([ ('name', '=', '{}_odoo_servers.conf'.format(protocol)) ]) conf.unlink() return # We have servers, create config files. for current_server in servers: current_server_secret = md5.md5('{}-{}'.format( current_server.id, current_server.uid)).hexdigest() conf_data = u'' other_servers = self.search([('is_interconnected', '=', True), ('id', '!=', current_server.id)]) for server in other_servers: server_secret = md5.md5('{}-{}'.format( server.id, server.uid)).hexdigest() conf_data += self.env['ir.qweb'].render( 'asterisk_base.server_{}_peer'.format(protocol), { 'server': server, 'current_server': current_server, 'server_secret': server_secret, 'current_server_secret': current_server_secret, }).decode('latin-1') conf = self.env['asterisk.conf'].get_or_create( current_server.id, '{}_odoo_servers.conf'.format(protocol)) conf.write({'content': conf_data}) conf.include_from('{}.conf'.format(protocol))
def challenge(self, stale=''): """ Returns the http headers that ask for appropriate authorization. """ nonce = md5.md5("%s:%s" % (time.time(), random.random())).hexdigest() opaque = md5.md5("%s:%s" % (time.time(), random.random())).hexdigest() self.nonce[nonce] = None parts = {'realm': self.realm, 'qop': 'auth', 'nonce': nonce, 'opaque': opaque } if stale: parts['stale'] = 'true' head = ", ".join(['%s="%s"' % (k, v) for (k, v) in parts.items()]) return HttpResponseUnauthorized('Digest %s' % head)
def f_md5(str): try: rez = md5(str) except: rez = md5.md5(str) return rez
def foto_apresentador(self): try: import hashlib.md5 as md5 except ImportError: import md5 as md5 gravatar = "http://www.gravatar.com/avatar/" + md5.md5(self.email.lower()).hexdigest() + "?s=80" return '<img src="%s" />' % gravatar
def _send_item(self, channel, data): """ Send one item """ modified_rel_path, checksum = data modifiedpath = os.path.join(self._sourcedir, *modified_rel_path) try: f = open(modifiedpath, 'rb') data = f.read() except IOError: data = None # provide info to progress callback function modified_rel_path = "/".join(modified_rel_path) if data is not None: self._paths[modified_rel_path] = len(data) else: self._paths[modified_rel_path] = 0 if channel not in self._to_send: self._to_send[channel] = [] self._to_send[channel].append(modified_rel_path) if data is not None: f.close() if checksum is not None and checksum == md5.md5(data).digest(): data = None # not really modified else: # ! there is a reason for the interning: # sharing multiple copies of the file's data data = intern(data) self._report_send_file(channel.gateway, modified_rel_path) channel.send(data)
def __call__(self, mi, log): digest = md5.md5(mi.text).digest() if digest in self.unique: log.pass_test(SPAM) return "duplicate" self.unique[digest] = 1 return False
def test(): good = open("/etc/motd").read() c=Client(domain='test',trackers=['peter:7001','lois:7001']) c.delete('/etc/motd') c.send_file('/etc/motd', '/etc/motd') assert(c.get_file_data('/etc/motd') == good) c.delete('/etc/motd_0') c.rename("/etc/motd", "/etc/motd_0") for x in range(10): c.delete('/etc/motd_%d' % (x+1)) c.rename("/etc/motd_%d" % x, "/etc/motd_%d" % (x+1)) assert(c.get_file_data('/etc/motd_%d' % (x+1)) == good) want = 'f6b3f164c99761234858a4d2c12d0840' x=0 while 1: time.sleep(1) try : start = time.time() m = md5.md5() [m.update(p) for p in c.get_bigfile_iter('/isos//ubuntu-5.04-install-i386.iso')] end = time.time() x+=1 print time.ctime(), (m.hexdigest() == want), end-start, 'seconds', x except MogileFSError, e: if "unable to read all paths" in str(e): print time.ctime(), e continue
def resourceChanged(self, resource): """Given a resource calculate it's md5 and return true if it is different from the last time this method was called. If this is the first time this method is called for this resouce it will return true """ md5sum = md5.md5() path = self.pathForResource(resource) # get the previous digest if it exits oldDigest = self.digests.get(path, "") try: resourceFile = open(path) except IOError: return Application.CONFIG_MISSING for line in resourceFile: md5sum.update(line) resourceFile.close() digest = md5sum.digest() if digest == oldDigest: return False else: self.digests[path] = digest return True
def mates(conf, dlcs, *args, **opts): """The following was adapted from delicious_mates. http://www.aiplayground.org/artikel/delicious-mates/ % dlcs mates [max_mates[, min_bookmarks[, min_common]]] """ max_mates, min_bookmarks, min_common = map(int, opts['mates'].split(',')) delicious_users = {} posts = cached_posts(conf, dlcs, opts['keep_cache']) print "Getting mates for collection of %i bookmarks" % len(posts['posts']) print "\nUsers for each bookmark:" for i, post in enumerate(posts['posts']): hash = md5.md5(post['href']).hexdigest() #urlfeed = pydelicious.dlcs_feed('urlinfo', urlmd5=hash) posts = dlcs_feed('url', count='all', format='rss', urlmd5=hash) usernames = [e['author'] for e in posts['entries']] print " %i. %s (%i)" % (i+1, post['href'], len(usernames)) for username in usernames: if username != dlcs.user: delicious_users.setdefault(username, (0.0, 0)) (weight, num_common) = delicious_users[username] new_weight = weight + 1.0/math.log(len(usernames)+1.0) delicious_users[username] = (new_weight, num_common + 1) print "\n%i candidates from list of %i users" % (max_mates, len(delicious_users)) friends = {} for (username, (weight, num_common)) in value_sorted(delicious_users): if num_common >= min_common: num_bookmarks = float([e['summary'] for e in dlcs_feed('user_info', format='rss', username='******')['entries'] if e['id'] == 'items'][0]) print " %s (%i/%i)" % (username, num_common, num_bookmarks), if num_bookmarks >= min_bookmarks: friends[username] = (weight*(num_common/num_bookmarks), num_common, num_bookmarks) if len(friends) >= max_mates: break else: print time.sleep(1) print "\nTop %i del.icio.us mates:" % max_mates print "username".ljust(20), "weight".ljust(20), "# common bookmarks".ljust(20), "# total bookmarks".ljust(20), "% common" print "--------------------------------------------------------------------------------------------" for (username, (weight, num_common, num_total)) in value_sorted(friends)[:max_mates]: print username.ljust(20), print ("%.5f" % (weight*100)).ljust(20), print str(num_common).ljust(20), print str(int(num_total)).ljust(20), print "%.5f" % ((num_common/num_total)*100.0)
def run(self): from hashlib import md5 retval = sdist_parent.run(self) for archive in self.get_archive_files(): data = file(archive, "rb").read() print '\n', archive, "\n\tMD5:", md5.md5(data).hexdigest() print "\tLength:", len(data) return retval
def POST(self): f = web.input(regNickname="", regUserName="", regEmail="", regUserPassword="", regUserPassword2="") users.create_account(f.regUserName, f.regEmail, f.regUserPassword, f.regNickname) #用户表入库 token = md5.md5(time.ctime() + f.regEmail).hexdigest() email_templates.create_account(f.regEmail, token) users.save_confirm_email(f.regEmail, token) session.login(f.regEmail) raise web.seeother('/')
def challenge(self, stale=''): """ Returns the http headers that ask for appropriate authorization. """ nonce = md5.md5("%s:%s" % (time.time(), random.random())).hexdigest() opaque = md5.md5("%s:%s" % (time.time(), random.random())).hexdigest() self.nonce[nonce] = None parts = { 'realm': self.realm, 'qop': 'auth', 'nonce': nonce, 'opaque': opaque } if stale: parts['stale'] = 'true' head = ", ".join(['%s="%s"' % (k, v) for (k, v) in parts.items()]) return HttpResponseUnauthorized('Digest %s' % head)
def insert(self, image_path, image_hash): _digest = md5.md5(image_path) image_id = str(_digest.hexdigest()) query = 'INSERT INTO image (image_id, image_path, image_hash) VALUES (?, ?, ?)' self.con.execute(query, ( image_id, image_path, image_hash, )) self.con.commit()
def render(self, context): try: email = self.email.resolve(context) except template.VariableDoesNotExist: return '' size = 80 gravatar_url = "http://www.gravatar.com/avatar/" + md5.md5(email.lower()).hexdigest() + "?s=" + str(size) return gravatar_url
def __init__(self, name, sequence): try: assert "\n" not in name assert "\n" not in sequence assert self.DELIMITER not in sequence self._name = name self._sequence = sequence self._md5 = md5.md5(self.sequence).hexdigest() self._id, self._metadata = splitFastaHeader(name) except AssertionError: raise ValueError("Invalid FASTA record data")
def uuid1(): t = int( time.time() * 1000 ) r = int( random.random()*100000000000000000 ) try: a = socket.gethostbyname( socket.gethostname() ) except: # if we can't get a network address, just imagine one a = random.random()*100000000000000000 data = str(t)+' '+str(r)+' '+str(a) data = md5.md5(data).hexdigest() return "%s-%s-%s-%s-%s" % (data[0:8], data[8:12], data[12:16], data[16:20], data[20:32])
def audit(arg): version = {'a57bd73e27be03a62dd6b3e1b537a72c':'4.0.0 - 4.2.3', '4b2c92409cf0bcf465d199e93a15ac3f':'4.3.0 - 4.3.10', '50caaf268b4f3d260d720a1a29c5fe21':'4.3.11 - 4.4.6; and 5.0.4 - 5.1.2', '85be3b4be7bfe839cbb3b4f2d30ff983':'5.0.0 - 5.0.3', '37e194b799d4aaff10e39c4e3b2679a2':'5.1.3 - 5.2.13', 'fb3bbd9ccc4b3d9e0b3be89c5ff98a14':'5.3.0 - current'} url = arg + "/?=PHPE9568F36-D428-11d2-A769-00AA001ACF42" code, _, body, _, _ = curl.curl(url) if code == 200: ver_md5 = md5.md5(body).hexdigest() if version.get(ver_md5): security_note('php version:%s' % version[ver_md5])
def jcalg_decompress(): import jcalg data = testdata.jcalg1[10:] # skip header blz = jcalg.decompress(data) decomp, offset = blz.do() m = md5.md5(decomp).hexdigest() if m != "7cda56f22188840f178efeebfb01f6b1": print "jcalg decompression error" else: pass
def send_bigfile(self, key, source, clas=None, description="", overwrite=True, chunksize=1024*1024*16): #default to saved? if clas is None: clas = self.clas prekey = "_big_pre:" + key infokey = "_big_info:" + key if prekey in self or infokey in self: if not overwrite: self._fail("pre file or info file for %s already exists") self.delete_small(prekey) self.delete_small(infokey) self[prekey] = "starttime:%d" % int(time.time()) opened = False if not hasattr(source, 'read'): source = open(source) opened = True chunkinfo = [] cnum = 1 totalsize = 0 oldclass = self.clas # i can't pass class to setitem while 1: self._debug("Buffering...") chunk = source.read(chunksize) if not chunk: break md5sum = md5.md5(chunk).hexdigest() size = len(chunk) ckey = "%s,%d" % (key, cnum) chunkinfo.append((md5sum, size, ckey)) self.set_file_data(ckey, chunk, clas) cnum +=1 totalsize += size info = self._make_bigfile_info(key, description, totalsize, chunkinfo) self[infokey] = info self.delete_small(prekey) if opened: source.close() return True
def uuid( *args ): """ Generates a universally unique ID. Any arguments only create more randomness. """ t = long( time.time() * 1000 ) r = long( random.random()*100000000000000000L ) try: a = socket.gethostbyname( socket.gethostname() ) except: # if we can't get a network address, just imagine one a = random.random()*100000000000000000L data = str(t)+' '+str(r)+' '+str(a)+' '+str(args) data = md5.md5(data).hexdigest() return data
def uuid(self, *args): """ Generates a universally unique ID. Any arguments only create more randomness. """ t = int(time.time() * 1000) r = int(random.random() * 100000000000000000) try: a = socket.gethostbyname(socket.gethostname()) except: # if we can't get a network address, just imagine one a = random.random() * 100000000000000000 data = str(t) + ' ' + str(r) + ' ' + str(a) + ' ' + str(args) data = md5.md5(data).hexdigest() return data
def create_default_profile_image(sender, created, instance, **kwargs): profile, prof_created = ProfileImage.objects.get_or_create(user=instance) if prof_created: conn = httplib.HTTPConnection("www.gravatar.com") conn.request( "GET", "/avatar/%s?s=256" % md5.md5(profile.user.email.lower()).hexdigest(), None, {"Accept": "image/gif"}) r = conn.getresponse() data = r.read() filename = '%s%s.%s' % (settings.PROFILE_IMAGE_UPLOAD_DIR, instance.id, 'png') arq = open('%s/%s' % (settings.MEDIA_ROOT, filename), "wb") arq.write(data) arq.close() conn.close() profile.image = filename profile.save()
def fpass(request): context = {'title': ' Восстановление пароля '} if request.method == "POST": username = request.POST['login'] try: user = User.objects.get(username=username) rpass = Rpass.objects.get_or_create(user=user)[0] #fixme key = md5.md5(username).hexdigest() rpass.key = key rpass.save() text = _(u'Для изменения пароля перейдите по ссылке:\n\n' u'http://gorodkamer.ru/sn/%s/\n\n' u'Игнориуйте данное сообщение если вы не востанавливали пароль') % key send_mail(_(u'Смена пароля Email gorodkamer.ru'), text, '*****@*****.**', [user.email], fail_silently=True) request.session['alert'] = _(u'Инструкции по смене пароля высланы на ваш Email') return HttpResponseRedirect(reverse('gk.views.index')) except user.DoesNotExist: request.session['alert'] = _(u'Данный пользователь не зарегестрирован') return render(request, 'fpass.html', context) return render(request, 'fpass.html', context)
def _set_xauth(self, servernum): paths = os.environ.get('PATH').split(':') for path in paths: if os.path.exists(os.path.join(path, "xauth")): break else: raise AssertError("Unable to find xauth in PATH") jhfolder = os.path.join(tempfile.gettempdir(), 'soup.%d' % os.getpid()) if os.path.exists(jhfolder): raise AssertError("Soup Xvfb folder already exists") try: os.mkdir(jhfolder) new_xauth = os.path.join(jhfolder, 'Xauthority') open(new_xauth, 'w').close() hexdigest = md5.md5(str(random.random())).hexdigest() os.system('xauth -f "%s" add ":%s" "." "%s"' % ( new_xauth, servernum, hexdigest)) except OSError: raise AssertError("Unable to setup XAuth") return new_xauth
def f_md5(str): try: rez = md5(str) except: rez = md5.md5(str) return rez.hexdigest()
return -1 # make sure we have md5. some systems don't have it try: from hashlib import md5 # Even if hashlib.md5 exists, it may be unusable. # Try to use MD5 function. In FIPS mode this will cause an exception # and we'll get to the replacement code foo = md5('abcd') except: try: import md5 # repeat the same check here, mere success of import is not enough. # Try to use MD5 function. In FIPS mode this will cause an exception foo = md5.md5('abcd') except: import Constants Constants.SIG_NIL = hash('abcd') class replace_md5(object): def __init__(self): self.val = None def update(self, val): self.val = hash((self.val, val)) def digest(self): return str(self.val) def hexdigest(self): return self.digest().encode('hex') def replace_h_file(filename): f = open(filename, 'rb') m = replace_md5()
def mates(conf, dlcs, *args, **opts): """The following was adapted from delicious_mates. http://www.aiplayground.org/artikel/delicious-mates/ % dlcs mates [max_mates[, min_bookmarks[, min_common]]] """ max_mates, min_bookmarks, min_common = map(int, opts['mates'].split(',')) delicious_users = {} posts = cached_posts(conf, dlcs, opts['keep_cache']) print "Getting mates for collection of %i bookmarks" % len(posts['posts']) print "\nUsers for each bookmark:" for i, post in enumerate(posts['posts']): hash = md5.md5(post['href']).hexdigest() #urlfeed = pydelicious.dlcs_feed('urlinfo', urlmd5=hash) posts = dlcs_feed('url', count='all', format='rss', urlmd5=hash) usernames = [e['author'] for e in posts['entries']] print " %i. %s (%i)" % (i + 1, post['href'], len(usernames)) for username in usernames: if username != dlcs.user: delicious_users.setdefault(username, (0.0, 0)) (weight, num_common) = delicious_users[username] new_weight = weight + 1.0 / math.log(len(usernames) + 1.0) delicious_users[username] = (new_weight, num_common + 1) print "\n%i candidates from list of %i users" % (max_mates, len(delicious_users)) friends = {} for (username, (weight, num_common)) in value_sorted(delicious_users): if num_common >= min_common: num_bookmarks = float([ e['summary'] for e in dlcs_feed( 'user_info', format='rss', username='******')['entries'] if e['id'] == 'items' ][0]) print " %s (%i/%i)" % (username, num_common, num_bookmarks), if num_bookmarks >= min_bookmarks: friends[username] = (weight * (num_common / num_bookmarks), num_common, num_bookmarks) if len(friends) >= max_mates: break else: print time.sleep(1) print "\nTop %i del.icio.us mates:" % max_mates print "username".ljust(20), "weight".ljust(20), "# common bookmarks".ljust( 20), "# total bookmarks".ljust(20), "% common" print "--------------------------------------------------------------------------------------------" for (username, (weight, num_common, num_total)) in value_sorted(friends)[:max_mates]: print username.ljust(20), print("%.5f" % (weight * 100)).ljust(20), print str(num_common).ljust(20), print str(int(num_total)).ljust(20), print "%.5f" % ((num_common / num_total) * 100.0)
def setpassword(self, newpassword): """Set an admin's password to a new value""" self.passwordhash = md5.md5(newpassword+pylons.config['debshots.md5salt']).hexdigest()
def make_unique_md5(): return md5.md5(time.ctime() + config.encryption_key).hexdigest()
def md5(password, secret=''): result = _md5.md5(password) result.update(secret) result = result.hexdigest return result
def goPartGetToken(self): import random, hashlib.md5 r = str(random.randint(100000, 999999)) token = md5.md5(r).hexdigest() return token
ip1 += is1; op += os; } } static PyUFuncGenericFunction f_functions[] = { PyUFunc_0, }; static char f_types[] = { NPY_DOUBLE, NPY_DOUBLE, NPY_BOOL, }; ''' ufunc_info = weave.base_info.custom_info() ufunc_info.add_header('"numpy/ufuncobject.h"') mandel = weave.inline('/*' + md5.md5(support_code).hexdigest() + '''*/ import_ufunc(); return_val = PyUFunc_FromFuncAndData(f_functions, NULL, f_types, 1, /* ntypes */ 2, /* nin */ 1, /* nout */ PyUFunc_None, /* identity */ "mandel", /* name */ "doc", /* doc */ 0); ''', support_code=support_code, verbose=0,
status = os.system(cmd) if os.WIFEXITED(status): return os.WEXITSTATUS(status) if os.WIFSIGNALED(status): return - os.WTERMSIG(status) Logs.error("Unknown exit reason %d for command: %s" (status, cmd)) return -1 # make sure we have md5. some systems don't have it try: from hashlib import md5 # Even if hashlib.md5 exists, it may be unusable. # Try to use MD5 function. In FIPS mode this will cause an exception # and we'll get to the replacement code foo = md5.md5('abcd') except: try: import md5 # repeat the same check here, mere success of import is not enough. # Try to use MD5 function. In FIPS mode this will cause an exception foo = md5.md5('abcd') except: import Constants Constants.SIG_NIL = hash('abcd') class replace_md5(object): def __init__(self): self.val = None def update(self, val): self.val = hash((self.val, val)) def digest(self):
def __init__(self, destination, params): self.rshpath = params['rsh_path'] self.user = params['user'] RemoteCommand.__init__(self, destination, params) self.delim = md5.md5(str(random.random())).hexdigest()
def digest_password(realm, username, password): """ Construct the appropriate hashcode needed for HTTP digest """ return md5.md5("%s:%s:%s" % (username, realm, password)).hexdigest()
return result # make sure we have md5. some systems don't have it try: from hashlib import md5 # Even if hashlib.md5 exists, it may be unusable. # Try to use MD5 function. In FIPS mode this will cause an exception # and we'll get to the replacement code foo = md5(b'abcd') except: try: import md5 # repeat the same check here, mere success of import is not enough. # Try to use MD5 function. In FIPS mode this will cause an exception foo = md5.md5(b'abcd') except: Context.SIG_NIL = hash('abcd') class replace_md5(object): def __init__(self): self.val = None def update(self, val): self.val = hash((self.val, val)) def digest(self): return str(self.val) def hexdigest(self): return self.digest().encode('hex') def replace_h_file(filename): f = open(filename, 'rb') m = replace_md5() while (filename):
def buildMappingsHashes(maps): return [(md5.md5(m.mmap().get_byte_buffer()).hexdigest(), m.mmap()) for m in maps]
def _get_path_data(self, paths, fp=None, timeout=5, checksum=None): """given a key, returns a scalar reference pointing at a string containing the contents of the file. if fp is specified, the file contents will be written to it... for HTTP this should lower memory usage.""" md5err = ioerr = 0 # iterate over each for path in paths + paths: #try each path twice? if path.startswith('http://'): # try via HTTP try : if fp is None: ofp = cStringIO.StringIO() else : ofp = fp curl = pycurl.Curl() curl.setopt(pycurl.URL, path) curl.setopt(pycurl.CONNECTTIMEOUT, timeout) #curl.setopt(pycurl.TIMEOUT, timeout) #next two lines: if I don't get 1 byte in 5 seconds, abort curl.setopt(pycurl.LOW_SPEED_TIME, 5) curl.setopt(pycurl.LOW_SPEED_LIMIT, 1) curl.setopt(pycurl.WRITEFUNCTION, ofp.write) curl.perform() curl.close() if fp is None: contents = ofp.getvalue() if not checksum or md5.md5(contents).hexdigest() == checksum: return contents else : self._debug("Checksum error on path %s" % path) md5err +=1 continue else : return True except pycurl.error: #ofp.seek(0) self._debug("IO error on path %s" % path) ioerr += 1 continue else: # open the file from disk and just grab it all try : f = open(path) contents = f.read() f.close() if fp is None: if not checksum or md5.md5(contents).hexdigest() == checksum: return contents else : self._debug("Checksum error on path %s" % path) md5err +=1 continue else : fp.write(contents) return True except IOError: self._debug("IO error on path %s" % path) if fp: fp.seek(0) ioerr +=1 continue return self._fail("unable to read all paths %s. ioerr: %d, md5err: %d" % (paths, ioerr, md5err))
# make sure we have md5. some systems don't have it try: from hashlib import md5 # Even if hashlib.md5 exists, it may be unusable. # Try to use MD5 function. In FIPS mode this will cause an exception # and we'll get to the replacement code foo = md5("abcd") except: try: import md5 # repeat the same check here, mere success of import is not enough. # Try to use MD5 function. In FIPS mode this will cause an exception foo = md5.md5("abcd") except: import Constants Constants.SIG_NIL = hash("abcd") class replace_md5(object): def __init__(self): self.val = None def update(self, val): self.val = hash((self.val, val)) def digest(self): return str(self.val)