示例#1
0
  def CreateEncryptingStreamWriter(self, output_stream,
                                   encoder=util.IncrementalBase64WSStreamWriter
                                  ):
    """
    Create an encrypting stream capable of writing a ciphertext byte stream
    containing Header|IV|Ciph|Sig.

    @param output_stream: target stream for encrypted output
    @type output_stream: 'file-like' object

    @param encoder: the encoding stream to use on the ciphertext stream.
    Defaults to base64 encoding with no padding or line breaks.
    Use None for raw bytes.
    @type encoder: 'file-like' object

    @return: an encrypting stream capable of creating a ciphertext byte stream
    @rtype: EncryptingStreamWriter
    """
    encrypting_key = self.primary_key
    if encrypting_key is None:
      raise errors.NoPrimaryKeyError()
    if encoder:
      stream = encoder(output_stream)
    else:
      stream = output_stream
    return keys.EncryptingStreamWriter(encrypting_key, stream)
示例#2
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))
示例#3
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))
示例#4
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))
示例#5
0
  def Encrypt(self, data, encoder=util.Base64WSEncode):
    """
    Encrypt the data and return the ciphertext.

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

    @param encoder: function to perform final encoding. Defaults to Base64, use
    None for no encoding.
    @type encoder: function

    @return: ciphertext, by default Base64 encoded
    @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()
    ciphertext = encrypting_key.Encrypt(data)
    return encoder(ciphertext) if encoder else ciphertext
示例#6
0
 def __InternalSign(self, data, nonce = None):
   signing_key = self.primary_key
   if signing_key is None:
     raise errors.NoPrimaryKeyError()
   return signing_key.Sign(data + util.PackByteArray(nonce) + VERSION_BYTE)