Example #1
0
    def upload(self, device, version, firmware):
        """Upload a firmware image to the system."""
        version = int(version)

        dev_id = self.__find_device(device)
        if dev_id == False:
            return {"ERROR": "Device '%s' not found" % device}

        # Check the version is correct
        r = model.FirmwareBlobs.selectBy(device=dev_id, version=version)
        if r.count() == 0:
            return {"ERROR": "Version %i does not exist" % version}
        fw = r[0]

        if self.__get_state(fw.id) != "ALLOCATED":
            return {"ERROR": "Firmware already uploaded"}

        data = firmware.file.read()

        s = sha.new()
        s.update(data)

        # Use the SHA1 as the filename
        fw.firmware = s.hexdigest()

        f = open("%s/%s" % (config.get("firmware.dir"), fw.firmware), "w")
        f.write(data)
        f.close()

        self.__add_state(fw.id, "Firmware uploaded", "DEVEL")

        return {"sha1": s.hexdigest()}
Example #2
0
 def randomize(self, N=0):
     "Adds N bits of entropy to random pool.  If N is 0, fill up pool."
     import os
     import string
     import time
     if N <= 0:
         bits = self.bits - self.entropy
     else:
         bits = N * 8
     if bits == 0:
         return
     print bits, 'bits of entropy are now required.  Please type on the keyboard'
     print 'until enough randomness has been accumulated.'
     kb = KeyboardEntry()
     s = ''    # We'll save the characters typed and add them to the pool.
     hash = self._hash
     e = 0
     try:
         while e < bits:
             temp = str(bits - e).rjust(6)
             os.write(1, temp)
             s = s + kb.getch()
             e += self.add_event(s)
             os.write(1, 6 * chr(8))
         self.add_event(s + hash.new(s).digest())
     finally:
         kb.close()
     print '\n\007 Enough.  Please wait a moment.\n'
     self.stir_n()   # wash the random pool.
     kb.close(4)
Example #3
0
	def sig( self ):
		"""Returns this site signature as a string. The signature uniquely
		identifies this site. Each instance has a different signature."""
		sig =  self.pages() + self.output() + self.templatesDir
		try:      s=hashfunc.new(sig).hexdigest()
		except:   s=hashfunc(sig).hexdigest()
		return s
Example #4
0
    def info(self, device, version):
        """Return information about the given firmware image version."""
        version = int(version)

        dev_id = self.__find_device(device)
        if dev_id == False:
            return {"ERROR": "Device '%s' not found" % device}

        r = model.FirmwareBlobs.selectBy(device=dev_id, version=version)
        if r.count() == 0:
            return {"ERROR": "Version %i does not exist" % version}
        fw = r[0]

        r = model.FirmwareState.selectBy(fw_id=fw.id).orderBy("date")
        log = []
        for entry in r:
            log.append({
                "time": datetime_to_stamp(entry.date),
                "message": entry.message,
                "state": entry.state
            })

        info = {"desc": fw.description, "state": r[-1].state, "log": log}

        if info["state"] != "ALLOCATED":
            "The hash and size only make sense once the file's been uploaded "
            h = sha.new()
            f = open("%s/%s" % (config.get("firmware.dir"), fw.firmware), "r")
            h.update(f.read())

            info["sha1"] = h.hexdigest()
            info["size"] = os.path.getsize(
                "%s/%s" % (config.get("firmware.dir"), fw.firmware))

        return info
Example #5
0
def encode_file_not_really_and_hash(inf, cb, k, m, chunksize=4096):
    hasher = sha1.new()
    enc = zfec.Encoder(k, m)
    l = tuple([ array.array('c') for i in range(k) ])
    indatasize = k*chunksize # will be reset to shorter upon EOF
    eof = False
    ZEROES=array.array('c', ['\x00'])*chunksize
    while not eof:
        # This loop body executes once per segment.
        i = 0
        while (i<len(l)):
            # This loop body executes once per chunk.
            a = l[i]
            del a[:]
            try:
                a.fromfile(inf, chunksize)
                i += 1
            except EOFError:
                eof = True
                indatasize = i*chunksize + len(a)

                # padding
                a.fromstring("\x00" * (chunksize-len(a)))
                i += 1
                while (i<len(l)):
                    a = l[i]
                    a[:] = ZEROES
                    i += 1

        # res = enc.encode(l)
        for thing in l:
            hasher.update(thing)
        cb(None, None)
Example #6
0
    def _heading_repl(self, word):
        """Handle section headings."""
        from hashlib import sha1

        h = word.strip()
        level = 1
        while h[level:level+1] == '=':
            level += 1
        depth = min(5,level)

        # this is needed for Included pages
        # TODO but it might still result in unpredictable results
        # when included the same page multiple times
        title_text = h[level:-level].strip()
        pntt = self.formatter.page.page_name + title_text
        self.titles.setdefault(pntt, 0)
        self.titles[pntt] += 1

        unique_id = ''
        if self.titles[pntt] > 1:
            unique_id = '-%d' % self.titles[pntt]
        result = self._closeP()
        result += self.formatter.heading(1, depth, id="head-"+sha1.new(pntt.encode(config.charset)).hexdigest()+unique_id)

        return (result + self.formatter.text(title_text) +
                self.formatter.heading(0, depth))
Example #7
0
 def randomize(self, N = 0):
     "Adds N bits of entropy to random pool.  If N is 0, fill up pool."
     import os, string, time
     if N <= 0:
         bits = self.bits - self.entropy
     else:
         bits = N*8
     if bits == 0:
         return
     print bits,'bits of entropy are now required.  Please type on the keyboard'
     print 'until enough randomness has been accumulated.'
     kb = KeyboardEntry()
     s=''    # We'll save the characters typed and add them to the pool.
     hash = self._hash
     e = 0
     try:
         while e < bits:
             temp=str(bits-e).rjust(6)
             os.write(1, temp)
             s=s+kb.getch()
             e += self.add_event(s)
             os.write(1, 6*chr(8))
         self.add_event(s+hash.new(s).digest() )
     finally:
         kb.close()
     print '\n\007 Enough.  Please wait a moment.\n'
     self.stir_n()   # wash the random pool.
     kb.close(4)
Example #8
0
def encode_file_not_really_and_hash(inf, cb, k, m, chunksize=4096):
    hasher = sha1.new()
    enc = zfec.Encoder(k, m)
    l = tuple([ array.array('c') for i in range(k) ])
    indatasize = k*chunksize # will be reset to shorter upon EOF
    eof = False
    ZEROES=array.array('c', ['\x00'])*chunksize
    while not eof:
        # This loop body executes once per segment.
        i = 0
        while (i<len(l)):
            # This loop body executes once per chunk.
            a = l[i]
            del a[:]
            try:
                a.fromfile(inf, chunksize)
                i += 1
            except EOFError:
                eof = True
                indatasize = i*chunksize + len(a)

                # padding
                a.fromstring("\x00" * (chunksize-len(a)))
                i += 1
                while (i<len(l)):
                    a = l[i]
                    a[:] = ZEROES
                    i += 1

        # res = enc.encode(l)
        for thing in l:
            hasher.update(thing)
        cb(None, None)
Example #9
0
 def save(self,force_insert=False,force_update=False):
     self.response = self.response.lower()
     if not self.expiration:
         self.expiration = datetime.datetime.now() + datetime.timedelta(minutes= int(captcha_settings.CAPTCHA_TIMEOUT))
     if not self.hashkey:
         self.hashkey = sha.new(str(self.challenge) + str(self.response)).hexdigest()
     super(CaptchaStore,self).save(using='megavideo'force_insert=force_insert,force_update=force_update)
Example #10
0
    def _heading_repl(self, word):
        """Handle section headings."""
        from hashlib import sha1

        h = word.strip()
        level = 1
        while h[level:level+1] == '=':
            level += 1
        depth = min(5,level)

        # this is needed for Included pages
        # TODO but it might still result in unpredictable results
        # when included the same page multiple times
        title_text = h[level:-level].strip()
        pntt = self.formatter.page.page_name + title_text
        self.titles.setdefault(pntt, 0)
        self.titles[pntt] += 1

        unique_id = ''
        if self.titles[pntt] > 1:
            unique_id = '-%d' % self.titles[pntt]
        result = self._closeP()
        result += self.formatter.heading(1, depth, id="head-"+sha1.new(pntt.encode(config.charset)).hexdigest()+unique_id)

        return (result + self.formatter.text(title_text) +
                self.formatter.heading(0, depth))
Example #11
0
def sha1sum(val):
    """Returns the SHA1 checksum of string val."""
    if hasattr(sha1, '__call__'):
        hash = sha1()
    else:
        hash = sha1.new()
    hash.update(val)
    return hash.hexdigest()
Example #12
0
File: utils.py Project: dat/pyatmos
def sha1sum(val):
    """Returns the SHA1 checksum of string val."""
    if hasattr(sha1, '__call__'):
        hash = sha1()
    else:
        hash = sha1.new()
    hash.update(val)
    return hash.hexdigest()
Example #13
0
    def plugin(self, owner):
        """ Determine the best auth method (digest/0k/plain) and use it for auth.
            Returns used method name on success. Used internally. """
        if not self.resource:
            return self.authComponent(owner)
        self.DEBUG("Querying server about possible auth methods", "start")
        resp = owner.Dispatcher.SendAndWaitForResponse(
            Iq("get", NS_AUTH, payload=[Node("username", payload=[self.user])])
        )
        if not isResultNode(resp):
            self.DEBUG("No result node arrived! Aborting...", "error")
            return
        iq = Iq(typ="set", node=resp)
        query = iq.getTag("query")
        query.setTagData("username", self.user)
        query.setTagData("resource", self.resource)

        if query.getTag("digest"):
            self.DEBUG("Performing digest authentication", "ok")
            query.setTagData(
                "digest", sha.new(owner.Dispatcher.Stream._document_attrs["id"] + self.password).hexdigest()
            )
            if query.getTag("password"):
                query.delChild("password")
            method = "digest"
        elif query.getTag("token"):
            token = query.getTagData("token")
            seq = query.getTagData("sequence")
            self.DEBUG("Performing zero-k authentication", "ok")
            hash = sha.new(sha.new(self.password).hexdigest() + token).hexdigest()
            for foo in xrange(int(seq)):
                hash = sha.new(hash).hexdigest()
            query.setTagData("hash", hash)
            method = "0k"
        else:
            self.DEBUG("Sequre methods unsupported, performing plain text authentication", "warn")
            query.setTagData("password", self.password)
            method = "plain"
        resp = owner.Dispatcher.SendAndWaitForResponse(iq)
        if isResultNode(resp):
            self.DEBUG("Sucessfully authenticated with remove host.", "ok")
            owner.User = self.user
            owner.Resource = self.resource
            owner._registered_name = owner.User + "@" + owner.Server + "/" + owner.Resource
            return method
        self.DEBUG("Authentication failed!", "error")
Example #14
0
    def sig(self):
        """Returns this site signature as a string. The signature uniquely
		identifies this site. Each instance has a different signature."""
        sig = self.pages() + self.output() + self.templatesDir
        try:
            s = hashfunc.new(sig).hexdigest()
        except:
            s = hashfunc(sig).hexdigest()
        return s
Example #15
0
 def authComponent(self, owner):
     """ Authenticate component. Send handshake stanza and wait for result. Returns "ok" on success. """
     self.handshake = 0
     owner.send(
         Node(
             NS_COMPONENT_ACCEPT + " handshake",
             payload=[sha.new(owner.Dispatcher.Stream._document_attrs["id"] + self.password).hexdigest()],
         )
     )
     owner.RegisterHandler("handshake", self.handshakeHandler, xmlns=NS_COMPONENT_ACCEPT)
     while not self.handshake:
         self.DEBUG("waiting on handshake", "notify")
         owner.Process(1)
     owner._registered_name = self.user
     if self.handshake + 1:
         return "ok"
Example #16
0
 def add_link(self,
              url,
              title,
              thumb,
              owner,
              color,
              timestamp,
              by_me=False):
     info = {
         'hash': sha1.new(str(url)).hexdigest(),
         'url': str(url),
         'title': str(title),
         'thumb': thumb,
         'owner': str(owner),
         'color': str(color),
         'timestamp': float(timestamp)
     }
     self.add_link_from_info(info, by_me)
Example #17
0
def encrypt(passwd):
    """
    Encrypts the incoming password after adding some salt to store
    it in the database.

    @param passwd: password portion of user credentials
    @type passwd: string

    @returns: encrypted/salted string
    """
    try:
        m = sha()
    except TypeError:
        m = sha.new()

    salt = os.urandom(salt_len).encode("hex_codec")
    m.update(passwd + salt)
    crypted = salt + m.hexdigest()
    return crypted
Example #18
0
def encrypt(passwd):
    """
    Encrypts the incoming password after adding some salt to store
    it in the database.

    @param passwd: password portion of user credentials
    @type passwd: string

    @returns: encrypted/salted string
    """
    try:
        m = sha()
    except TypeError:
        m = sha.new()

    salt = os.urandom(salt_len).encode('hex_codec')
    m.update(passwd + salt)
    crypted = salt + m.hexdigest()
    return crypted
Example #19
0
def check_passwd(guess, passwd):
    """
    Tests to see if the guess, after salting and hashing, matches the
    passwd from the database.

    @param guess: incoming password trying to be used for authentication
    @param passwd: already encrypted password from the database

    @returns: boolean
    """
    try:
        m = sha()
    except TypeError:
        m = sha.new()

    salt = passwd[:salt_len * 2]  # salt_len * 2 due to encode('hex_codec')
    m.update(guess + salt)
    crypted_guess = salt + m.hexdigest()

    return (crypted_guess == passwd)
Example #20
0
def check_passwd(guess, passwd):
    """
    Tests to see if the guess, after salting and hashing, matches the
    passwd from the database.

    @param guess: incoming password trying to be used for authentication
    @param passwd: already encrypted password from the database

    @returns: boolean
    """
    try:
        m = sha()
    except TypeError:
        m = sha.new()

    salt = passwd[: salt_len * 2]  # salt_len * 2 due to encode('hex_codec')
    m.update(guess + salt)
    crypted_guess = salt + m.hexdigest()

    return crypted_guess == passwd
Example #21
0
    def generateUniqueId(self):
        """Generate a new, random, unique id.

          >>> bim = CookieClientIdManager()
          >>> id1 = bim.generateUniqueId()
          >>> id2 = bim.generateUniqueId()
          >>> id1 != id2
          True

        """
        data = "%.20f%.20f%.20f" % (random.random(), time.time(), time.clock())
        # BBB code for Python 2.4, inspired by the fallback in hmac
        if hasattr(sha1, '__call__'):
            digest = sha1(data).digest()
        else:
            digest = sha1.new(data).digest()
        s = digestEncode(digest)
        # we store a HMAC of the random value together with it, which makes
        # our session ids unforgeable.
        mac = hmac.new(self.secret, s, digestmod=sha1).digest()
        return s + digestEncode(mac)
Example #22
0
MD5 = "MD5"
SHA1 = "SHA1"
MD5_SESS = "MD5-sess"
AUTH = "auth"
AUTH_INT = "auth-int"

SUPPORTED_ALGORITHM = (MD5, MD5_SESS)
SUPPORTED_QOP = (AUTH, AUTH_INT)

###############################################################################
# doAuth
#
DIGEST_AUTH_ENCODERS = {
    MD5: lambda val: md5(val).hexdigest(),
    MD5_SESS: lambda val: md5(val).hexdigest(),
    SHA1: lambda val: sha1.new(val).hexdigest(),
}


def calculateNonce(realm, algorithm=MD5):
    """This is an auxaliary function that calculates 'nonce' value. It is used
    to handle sessions."""

    assert algorithm in SUPPORTED_ALGORITHM

    try:
        encoder = DIGEST_AUTH_ENCODERS[algorithm]
    except KeyError:
        raise NotImplementedError("The chosen algorithm (%s) does not have "
                                  "an implementation yet" % algorithm)
Example #23
0
MD5 = "MD5"
SHA1 = "SHA1"
MD5_SESS = "MD5-sess"
AUTH = "auth"
AUTH_INT = "auth-int"

SUPPORTED_ALGORITHM = (MD5, MD5_SESS)
SUPPORTED_QOP = (AUTH, AUTH_INT)

###############################################################################
# doAuth
#
DIGEST_AUTH_ENCODERS = {
    MD5: lambda val: md5(val).hexdigest(),
    MD5_SESS: lambda val: md5(val).hexdigest(),
    SHA1: lambda val: sha1.new(val).hexdigest(),
}


def calculateNonce(realm, algorithm=MD5):
    """This is an auxaliary function that calculates 'nonce' value. It is used
    to handle sessions."""

    assert algorithm in SUPPORTED_ALGORITHM

    try:
        encoder = DIGEST_AUTH_ENCODERS[algorithm]
    except KeyError:
        raise NotImplementedError(
            "The chosen algorithm (%s) does not have "
            "an implementation yet" % algorithm
Example #24
0
def _wsse_username_token(cnonce, iso_now, password):
    return base64.encodestring(
        sha.new("%s%s%s" % (cnonce, iso_now, password)).digest()).strip()
Example #25
0
 def hash(s):
   return sha.new(s).hexdigest()
Example #26
0
    def hasChanged(self, path):
        """Tells wether the given resource has changed since last build for this
		website or not. The path is converted to an absolute location, so
		moving the website directory will cause a rebuild."""
        # Maybe we already know if the path has changed
        path = os.path.abspath(path)
        res = self.changed.get(path)
        if not self.site.isTemplate(path) and res != None:
            return res
        data = None

        def load_data(path):
            fd = file(path, 'r')
            res = fd.read()
            fd.close()
            return res

        # Is the page a template ?
        template_has_changed = res or False
        if self.site.isTemplate(path):
            data = load_data(path)
            # If so, we look for the extends defintion
            template = None
            for line in data.split("\n"):
                line = line.strip()
                if line and not line.startswith("##") and not line.startswith(
                        "#extends"):
                    break
                if line.startswith("#extends"):
                    template = line.strip()[len("#extends"):].strip()
                # If the template was flagged with ALWAYS_REBUILD, then we force
                # the build
                if RE_ALWAYS_REBUILD.match(line):
                    template_has_changed = True
                    break
                depends = RE_DEPENDS.match(line)
                # Handles dependencies
                if depends:
                    dep_path = depends.group(1).strip()
                    dep_path = os.path.expanduser(dep_path)
                    dep_abspath = os.path.abspath(dep_path)
                    # The path may be relative to the current path
                    if dep_abspath != dep_path:
                        dep_abspath = os.path.abspath(
                            os.path.dirname(path) + "/" + dep_path)
                    dep_path = dep_abspath
                    for dependency in glob.glob(dep_path):
                        if self.hasChanged(dependency):
                            template_has_changed = True
                            break
                    if template_has_changed: break
            # If there was a template extended, we check if it is present in the
            # templates directory
            if template and template.startswith("Templates"):
                template_path = apply(os.path.join, template.split(".")[1:])
                template_path = os.path.join(self.site.templatesDir,
                                             template_path + ".tmpl")
                # And if this template has changed, then this one too
                if self.hasChanged(template_path):
                    template_has_changed = True
        # There is a SHA1 mode for real checksum change detection
        if self.site.changeDetectionMethod() == CHANGE_CHECKSUM:
            try:  # sha1
                chksum = hashfunc.new(data or load_data(path)).hexdigest()
            except:
                chksum = hashfunc(data
                                  or load_data(path)).hexdigest()  # hashlib
        # Default is modification time (faster)
        else:
            if os.path.exists(path):
                chksum = os.stat(path)[stat.ST_MTIME]
            else:
                warn("Path does not exists: " + path)
                chksum = "0"
        # We get the previous checksum
        checksums = self.checksums.get(self.site.sig())
        if checksums:
            old_checksum = checksums.get(path)
        else:
            old_checksum = None
        # Then we compare to registered checksums
        # If the checksum has changed
        if template_has_changed or old_checksum != chksum:
            # We take care of the mode
            if not self.checksums.get(self.site.sig()):
                self.checksums[self.site.sig()] = {}
            self.checksums[self.site.sig()][path] = chksum
            self.changed[path] = True
            return True
        else:
            self.changed[path] = False
            return False
Example #27
0
def _wsse_username_token(cnonce, iso_now, password):
    return base64.encodestring(sha.new("%s%s%s" % (cnonce, iso_now, password)).digest()).strip()
Example #28
0
import sys
import os
import logging
import traceback
from ConfigParser import SafeConfigParser
from optparse import OptionParser
from optparse import SUPPRESS_HELP

import hmac
try:
    from hashlib import sha1 as sha
    from hashlib import sha256 as sha256

    sha.new()
except:
    import sha
import base64
import uuid

Version = "0.1"

def log(level, msg, tb=None):
    logging.log(level, msg)
    if tb != None:
        logging.log(level, "Stack trace")
        logging.log(level, "===========")
        stack = tb.format_exc()
        logging.log(level, stack)
        logging.log(level, "===========")
        logging.log(level, sys.exc_info()[0])
Example #29
0
 def hash(s):
     return sha.new(s).hexdigest()
Example #30
0
	def hasChanged( self, path ):
		"""Tells wether the given resource has changed since last build for this
		website or not. The path is converted to an absolute location, so
		moving the website directory will cause a rebuild."""
		# Maybe we already know if the path has changed
		path = os.path.abspath(path)
		res  = self.changed.get(path) 
		if not self.site.isTemplate(path) and res != None:
			return res
		data = None
		def load_data(path):
			fd  = file(path, 'r')
			res = fd.read()
			fd.close()
			return res
		# Is the page a template ?
		template_has_changed = res or False
		if self.site.isTemplate(path):
			data = load_data(path)
			# If so, we look for the extends defintion
			template = None
			for line in data.split("\n"):
				line = line.strip()
				if line and not line.startswith("##") and not line.startswith("#extends"): break
				if line.startswith("#extends"):
					template = line.strip()[len("#extends"):].strip()
				# If the template was flagged with ALWAYS_REBUILD, then we force
				# the build
				if RE_ALWAYS_REBUILD.match(line):
					template_has_changed = True
					break
				depends = RE_DEPENDS.match(line)
				# Handles dependencies
				if depends:
					dep_path = depends.group(1).strip()
					dep_path = os.path.expanduser(dep_path)
					dep_abspath = os.path.abspath(dep_path)
					# The path may be relative to the current path
					if dep_abspath != dep_path:
						dep_abspath = os.path.abspath(os.path.dirname(path) + "/" + dep_path)
					dep_path = dep_abspath
					for dependency in glob.glob(dep_path):
						if self.hasChanged(dependency):
							template_has_changed = True
							break
					if template_has_changed: break
			# If there was a template extended, we check if it is present in the
			# templates directory
			if template and template.startswith("Templates"):
				template_path = apply(os.path.join, template.split(".")[1:])
				template_path = os.path.join(self.site.templatesDir, template_path + ".tmpl")
				# And if this template has changed, then this one too
				if self.hasChanged(template_path):
					template_has_changed = True
		# There is a SHA1 mode for real checksum change detection
		if self.site.changeDetectionMethod() == CHANGE_CHECKSUM:
			try:															# sha1
				chksum = hashfunc.new(data or load_data(path)).hexdigest()
			except:
				chksum = hashfunc(data or load_data(path)).hexdigest() # hashlib
		# Default is modification time (faster)
		else:
			if os.path.exists(path):
				chksum  = os.stat(path)[stat.ST_MTIME]
			else:
				warn("Path does not exists: " + path)
				chksum  = "0"
		# We get the previous checksum
		checksums = self.checksums.get(self.site.sig())
		if checksums:
			old_checksum = checksums.get(path)
		else:
			old_checksum = None
		# Then we compare to registered checksums
		# If the checksum has changed
		if template_has_changed or old_checksum != chksum:
			# We take care of the mode
			if not self.checksums.get(self.site.sig()):
				self.checksums[self.site.sig()] = {}
			self.checksums[self.site.sig()][path] = chksum
			self.changed[path] = True
			return True
		else:
			self.changed[path] = False
			return False