Exemplo n.º 1
0
  def Generate(size=keyinfo.HMAC_SHA1.default_size):
    """
    Return a newly generated HMAC-SHA1 key.

    @param size: length of key in bits to generate
    @type size: integer

    @return: an HMAC-SHA1 key
    @rtype: L{HmacKey}
    """
    key_bytes = util.RandBytes(size / 8)
    key_string = util.Encode(key_bytes)
    return HmacKey(key_string, size)
Exemplo n.º 2
0
  def Generate(size=keyinfo.AES.default_size):
    """
    Return a newly generated AES key.

    @param size: length of key in bits to generate
    @type size: integer

    @return: an AES key
    @rtype: L{AesKey}
    """
    key_bytes = util.RandBytes(size / 8)
    key_string = util.Encode(key_bytes)
    hmac_key = HmacKey.Generate()  # use default HMAC-SHA1 key size
    return AesKey(key_string, hmac_key, size)
Exemplo n.º 3
0
 def __str__(self):
   return json.dumps({ "publicKey": json.loads(str(self.public_key)),
                      "privateExponent" : util.Encode(self.params['privateExponent']),
                      "primeP" : util.Encode(self.params['primeP']),
                      "primeQ" : util.Encode(self.params['primeQ']),
                      "primeExponentP" : util.Encode(self.params['primeExponentP']),
                      "primeExponentQ" : util.Encode(self.params['primeExponentQ']),
                      "crtCoefficient" : util.Encode(self.params['crtCoefficient']),
                      "size": self.size})
Exemplo n.º 4
0
    def Encrypt(self, data):
        """
    Encrypt the data and return the ciphertext.

    @param data: message to encrypt
    @type data: string

    @return: ciphertext encoded as a Base64 string
    @rtype: string

    @raise NoPrimaryKeyError: if no primary key can be found to encrypt
    """
        encrypting_key = self.primary_key
        if encrypting_key is None:
            raise errors.NoPrimaryKeyError()
        return util.Encode(encrypting_key.Encrypt(data))
Exemplo n.º 5
0
    def Sign(self, data):
        """
    Sign given data and return corresponding signature. This signature
    contains no header or version information.

    For message M, outputs the signature as Sig(M).

    @param data: message to be signed
    @type data: string

    @return: signature on the data encoded as a Base64 string
    @rtype: string
    """
        signing_key = self.primary_key
        if signing_key is None:
            raise errors.NoPrimaryKeyError()
        return util.Encode(signing_key.Sign(data))
Exemplo n.º 6
0
    def Sign(self, data):
        """
    Sign given data and return corresponding signature.

    For message M, outputs the signature as Header|Sig(Header.M).

    @param data: message to be signed
    @type data: string

    @return: signature on the data encoded as a Base64 string
    @rtype: string
    """
        signing_key = self.primary_key
        if signing_key is None:
            raise errors.NoPrimaryKeyError()
        header = signing_key.Header()
        return util.Encode(header + signing_key.Sign(data + VERSION_BYTE))
Exemplo n.º 7
0
    def _ParseHeader(self, header):
        """
    Parse the header and verify version, format info. Return key if exists.

    @param header: the bytes of the header of Keyczar output
    @type header: string

    @return: the key identified by the hash in the header
    @rtype: L{keys.Key}

    @raise BadVersionError: if header specifies an illegal version
    @raise KeyNotFoundError: if key specified in header doesn't exist
    """
        version = ord(header[0])
        if version != VERSION:
            raise errors.BadVersionError(version)
        hash = util.Encode(header[1:])
        return self.GetKey(hash)
Exemplo n.º 8
0
 def _Hash(self):
   fullhash = util.PrefixHash(util.TrimBytes(self._params['modulus']),
                              util.TrimBytes(self._params['publicExponent']))
   return util.Encode(fullhash[:keyczar.KEY_HASH_SIZE])
Exemplo n.º 9
0
 def __str__(self):
   return json.dumps({"modulus": util.Encode(self.params['modulus']),
                      "publicExponent": util.Encode(self.params['publicExponent']),
                      "size": self.size})
Exemplo n.º 10
0
 def _Hash(self):
   fullhash = util.PrefixHash(util.TrimBytes(self._params['p']),
                        util.TrimBytes(self._params['q']),
                        util.TrimBytes(self._params['g']),
                        util.TrimBytes(self._params['y']))
   return util.Encode(fullhash[:keyczar.KEY_HASH_SIZE])
Exemplo n.º 11
0
 def __str__(self):
   return json.dumps({"p": util.Encode(self.params['p']),
                      "q": util.Encode(self.params['q']),
                      "g": util.Encode(self.params['g']),
                      "y": util.Encode(self.params['y']),
                      "size": self.size})
Exemplo n.º 12
0
 def __str__(self):
   return json.dumps({"publicKey": json.loads(str(self.public_key)),
                      "x": util.Encode(self.params['x']),
                      "size": self.size})
Exemplo n.º 13
0
 def _Hash(self):
   fullhash = util.Hash(self.key_bytes)
   return util.Encode(fullhash[:keyczar.KEY_HASH_SIZE])
Exemplo n.º 14
0
 def _Hash(self):
   fullhash = util.Hash(util.IntToBytes(len(self.key_bytes)),
                        self.key_bytes,
                        self.hmac_key.key_bytes)
   return util.Encode(fullhash[:keyczar.KEY_HASH_SIZE])
Exemplo n.º 15
0
 def _Hash(self):
   """Compute and return the hash id of this key. Can override default hash."""
   fullhash = util.Hash(util.IntToBytes(len(self.key_bytes)), self.key_bytes)
   return util.Encode(fullhash[:keyczar.KEY_HASH_SIZE])