def fromString(self, content):
     """FromString digests the string input and returns a Digest.
     TODO not sure what this is intended for.
     """
     if not isinstance(content, str):
         bot.exit("input must be string")
     content = bytes(content, "utf-8")
     return self.fromBytes(content)
 def fromReader(self, ioReader):
     """FromReader returns the digest of the reader using the algorithm.
     the input must be type bytes, usually from io.BytesIO.read().
     This function likely isn't needed, but is provided to mirror
     the GoLang implementation.
     """
     if not isinstance(ioReader, io.BytesIO):
         bot.exit("input must be io.BytesIO")
     return self.fromBytes(ioReader.read())
Exemple #3
0
    def verifier(self):
        """Verifier returns a writer object that can be used to verify a stream of
           content against the digest. If the digest is invalid, the method will panic.
        """
        from .verifiers import hashVerifier

        hashObj = self.algorithm.hash()
        if not hashObj:
            bot.exit("Algorithm is not available")
        return hashVerifier(hashObj, digest=self)
    def add(self, name, value):
        """add a value to an existing attribute, normally when used by a client"""
        if name not in self.attrs:
            bot.exit("%s is not a valid attribute." % name)

        attr = self.attrs[name]

        # Don't validate the type if provided is empty
        if value:
            if not attr.set(value):
                bot.exit("%s must be type %s." % (name, attr.attType))
    def load(self, content, validate=True):
        """given a dictionary load into its respective object
        if validate is True, we require it to be completely valid.
        """
        # import code
        # code.interact(local=locals())

        if not isinstance(content, dict):
            bot.exit("Please provide a dictionary or list to load.")

        # Look up attributes based on jsonKey
        lookup = self.generate_json_lookup()

        for key, value in content.items():
            att = lookup.get(key)
            if not att:
                bot.exit("%s is not a valid json attribute." % key)

        # If we get here, all parameters are valid, replace
        self._clear_values()

        for key, value in content.items():
            att = lookup.get(key)
            valid = att.set(value)
            if not valid and validate:
                bot.exit("%s (%s) is not valid." % (att.name, att.jsonName))

        # Validate the entire structure
        if validate:
            if not self.validate():
                bot.exit("%s is invalid" % self)
        return self
Exemple #6
0
    def sepIndex(self):
        """return the index of the : separator or the index
           that separtes the extra content provided in the algorithm name.
        """
        try:
            algorithm, encoded = (self).split(":")
        except:
            bot.exit("empty digest or algorithm")

        # Empty algorithm or encoded portion
        if not algorithm or not encoded:
            bot.exit("empty digest or algorithm")

        match = re.search("[+._-]", algorithm)
        if match:
            return match.start()
        return self.index(":", 1)
    def validate(self):
        """Validate checks that the contents of self (the digest) is valid"""
        if not self:
            bot.exit("Empty digest")

        regexp = "^[a-z0-9]+(?:[+._-][a-z0-9]+)*:[a-zA-Z0-9=_-]+$"

        # Must match for a digest
        if not re.search(regexp, self):
            raise ErrDigestInvalidFormat()

        algorithm, encoded = (self).split(":")

        # Remove the extra component, if there
        match = re.search("[+._-]", algorithm)
        if match:
            algorithm = algorithm[:match.start()]
        algorithm = Algorithm(algorithm)

        # Also checks if algorithm.available()
        return algorithm.validate(encoded)
Exemple #8
0
    def load(self, content, validate=True):
        '''given a dictionary load into its respective object
           if validate is True, we require it to be completely valid.
        '''
        if not isinstance(content, dict):
            bot.exit("Please provide a dictionary to load.")

        # Look up attributes based on jsonKey
        lookup = self.generate_json_lookup()

        # Go through each value for the content, validate
        for key, value in content.items():
            att = lookup.get(key)
            if not att:
                bot.exit("%s is not a valid json attribute." % key)

        # If we get here, all parameters are valid, replace
        self._clear_values()
        for key, value in content.items():
            att = lookup.get(key)
            valid = att.set(value)
            if not valid and validate:
                bot.exit("%s (%s) is not valid." % (att.name, att.jsonName))
            self.attrs[att.name] = att

        # Return the updated instance
        return self