parameters: (target)
return_statement: return _d
docstring: Create an ASCII-armored block.
  
      :Parameters:
          - `target`: message instance, signature packet, or list of packets
  
      :Returns: string of armored data
  
      Message instances will be armored as their appropriate type, lone signature
      packets will be armored as a lone, clearsigned signature, and any other
      packet, list of packets, or arbitrary strings will be armored as
      'PGP FLOTSAM'.
  
      Clearsigned text is automatically produced for a signed message which has
      only text signatures (``SIG_TEXT``) on a literal message.
  
      :note: The theory behind armoring arbitrary strings is that armoring is
          just a synonym for "encode as base64." The 'PGP FLOTSAM' label should
          be enough to flag such things as non-standard.
      :note: Single packet messages (ex. literal message made up of one literal
          packet) should be sent as a message instance to avoid being labeled as
          "flotsam."
  
      :todo: Automatically try to armor complete messages in a list of packets.
      :todo: Make clearsigning an option. See clearsigning conditions above.
docstring_summary: Create an ASCII-armored block.
docstring_tokens: Create an ASCII - armored block .
function: def apply_armor(target):
      """Create an ASCII-armored block.
  
      :Parameters:
          - `target`: message instance, signature packet, or list of packets
  
      :Returns: string of armored data
  
      Message instances will be armored as their appropriate type, lone signature
      packets will be armored as a lone, clearsigned signature, and any other
      packet, list of packets, or arbitrary strings will be armored as
      'PGP FLOTSAM'.
  
      Clearsigned text is automatically produced for a signed message which has
      only text signatures (``SIG_TEXT``) on a literal message.
  
      :note: The theory behind armoring arbitrary strings is that armoring is
          just a synonym for "encode as base64." The 'PGP FLOTSAM' label should
          be enough to flag such things as non-standard.
      :note: Single packet messages (ex. literal message made up of one literal
          packet) should be sent as a message instance to avoid being labeled as
          "flotsam."
  
      :todo: Automatically try to armor complete messages in a list of packets.
      :todo: Make clearsigning an option. See clearsigning conditions above.
      """
      from openpgp.sap.pkt.Signature import Signature as SIG_CLASS
      from openpgp.sap.msg.Msg import Msg as MSG_CLASS
  
      clearsign = False
  
      if isinstance(target, SIG_CLASS):
          header_line = "-----BEGIN PGP SIGNATURE-----"
          footer_line = "-----END PGP SIGNATURE-----"
          data = target.rawstr()
  
      elif isinstance(target, MSG_CLASS):
  
          if target.type in [MSG_COMPRESSED, MSG_ENCRYPTED, MSG_LITERAL]:
              header_line = "-----BEGIN PGP MESSAGE-----"
              footer_line = "-----END PGP MESSAGE-----"
              data = target.rawstr()
  
          elif MSG_PRIVATEKEY == target.type:
              header_line = "-----BEGIN PGP PRIVATE KEY BLOCK-----"
              footer_line = "-----END PGP PRIVATE KEY BLOCK-----"
              data = target.rawstr()
  
          elif MSG_PUBLICKEY == target.type:
              header_line = "-----BEGIN PGP PUBLIC KEY BLOCK-----"
              footer_line = "-----END PGP PUBLIC KEY BLOCK-----"
              data = target.rawstr()
  
          elif MSG_SIGNED == target.type:
              header_line = "-----BEGIN PGP MESSAGE-----"
              footer_line = "-----END PGP MESSAGE-----"
              text_sigs = [s for s in target.sigs if SIG_TEXT == s.body.type]
              len_text_sigs = len(text_sigs)
  
              # make sure there are text sigs at all before considering clearsigs
              if len_text_sigs and MSG_LITERAL == target.msg.type and len_text_sigs == len(target.sigs):
                  clearsign = True
                  data = ''.join([l.body.data for l in target.msg.literals])
                  alg_hash = text_sigs[0].body.alg_hash
                  # just munge footer since "END PGP SIGNED MESSAGE" not used
                  sig_d_list = []
  
                  for text_sig in text_sigs:
                      sig_d_list.append(apply_armor(text_sig))
  
                      if alg_hash != text_sig.body.alg_hash:
                          raise PGPFormatError("Clearsigs require the same hash for each signature. 1st sig hash->(%s) conflicts with a following sig->(%s)" % (alg_hash, text_sig.body.alg_hash))
  
                  if HASH_SHA1 == alg_hash:
                      hash_header = "Hash: SHA1"
  
                  elif HASH_MD5 == alg_hash:
                      hash_header = "Hash: MD5"
  
                  header_line = os.linesep.join(["-----BEGIN PGP SIGNED MESSAGE-----", hash_header])
                  footer_line = os.linesep.join(sig_d_list)
  
              else:
                  data = target.rawstr()
  
          else: # dismiss marker and stored key messages
              raise NotImplementedError("Armoring of message type %s is not supported." % target.type)
  
      else: # handle funky lists of packets or strings
  
          if not isinstance(target, list):
              target = [target]
  
          header_line = "-----BEGIN PGP FLOTSAM-----"
          footer_line = "-----END PGP FLOTSAM-----"
  
          datalist = []
          for t in target:
  
              if hasattr(t, 'rawstr'):
                  t = t.rawstr()
  
              datalist.append(t)
  
          #data = ''.join([p.rawstr() for p in target])
          data = ''.join(datalist)
  
      if clearsign:
          armored_d = data
          if data[-1:] != '\n': # \r\n, whatever.. just use a pointless chksum
              checksumline = os.linesep # to ensure some line separation
          else:
              checksumline = ''
      else:
          armored_d = base64.encodestring(data)
          checksumline = '=' + base64.encodestring(STN.int2str(crc24(data))) + os.linesep
  
      _d = header_line + (os.linesep * 2) + armored_d + checksumline + footer_line + os.linesep
  
      return _d
function_tokens: def apply_armor ( target ) : from openpgp . sap . pkt . Signature import Signature as SIG_CLASS from openpgp . sap . msg . Msg import Msg as MSG_CLASS clearsign = False if isinstance ( target , SIG_CLASS ) : header_line = "-----BEGIN PGP SIGNATURE-----" footer_line = "-----END PGP SIGNATURE-----" data = target . rawstr ( ) elif isinstance ( target , MSG_CLASS ) : if target . type in [ MSG_COMPRESSED , MSG_ENCRYPTED , MSG_LITERAL ] : header_line = "-----BEGIN PGP MESSAGE-----" footer_line = "-----END PGP MESSAGE-----" data = target . rawstr ( ) elif MSG_PRIVATEKEY == target . type : header_line = "-----BEGIN PGP PRIVATE KEY BLOCK-----" footer_line = "-----END PGP PRIVATE KEY BLOCK-----" data = target . rawstr ( ) elif MSG_PUBLICKEY == target . type : header_line = "-----BEGIN PGP PUBLIC KEY BLOCK-----" footer_line = "-----END PGP PUBLIC KEY BLOCK-----" data = target . rawstr ( ) elif MSG_SIGNED == target . type : header_line = "-----BEGIN PGP MESSAGE-----" footer_line = "-----END PGP MESSAGE-----" text_sigs = [ s for s in target . sigs if SIG_TEXT == s . body . type ] len_text_sigs = len ( text_sigs ) # make sure there are text sigs at all before considering clearsigs if len_text_sigs and MSG_LITERAL == target . msg . type and len_text_sigs == len ( target . sigs ) : clearsign = True data = '' . join ( [ l . body . data for l in target . msg . literals ] ) alg_hash = text_sigs [ 0 ] . body . alg_hash # just munge footer since "END PGP SIGNED MESSAGE" not used sig_d_list = [ ] for text_sig in text_sigs : sig_d_list . append ( apply_armor ( text_sig ) ) if alg_hash != text_sig . body . alg_hash : raise PGPFormatError ( "Clearsigs require the same hash for each signature. 1st sig hash->(%s) conflicts with a following sig->(%s)" % ( alg_hash , text_sig . body . alg_hash ) ) if HASH_SHA1 == alg_hash : hash_header = "Hash: SHA1" elif HASH_MD5 == alg_hash : hash_header = "Hash: MD5" header_line = os . linesep . join ( [ "-----BEGIN PGP SIGNED MESSAGE-----" , hash_header ] ) footer_line = os . linesep . join ( sig_d_list ) else : data = target . rawstr ( ) else : # dismiss marker and stored key messages raise NotImplementedError ( "Armoring of message type %s is not supported." % target . type ) else : # handle funky lists of packets or strings if not isinstance ( target , list ) : target = [ target ] header_line = "-----BEGIN PGP FLOTSAM-----" footer_line = "-----END PGP FLOTSAM-----" datalist = [ ] for t in target : if hasattr ( t , 'rawstr' ) : t = t . rawstr ( ) datalist . append ( t ) #data = ''.join([p.rawstr() for p in target]) data = '' . join ( datalist ) if clearsign : armored_d = data if data [ - 1 : ] != '\n' : # \r\n, whatever.. just use a pointless chksum checksumline = os . linesep # to ensure some line separation else : checksumline = '' else : armored_d = base64 . encodestring ( data ) checksumline = '=' + base64 . encodestring ( STN . int2str ( crc24 ( data ) ) ) + os . linesep _d = header_line + ( os . linesep * 2 ) + armored_d + checksumline + footer_line + os . linesep return _d