parameters: (sigtype, key, **kw)
docstring: Create a signed message or detached signature instance.
  
      :Parameters:
          - `sigtype`: integer signature type constant
          - `key`: signing key message `openpgp.sap.msg.KeyMsg.SecretKeyMsg`
            instance
  
      :Keywords:
          - `passphrase`: string passphrase for secret signing key
          - `target`: message to sign
          - `target_key`: TUP (primary, keyid) key ID or fingerprint of key packet in `target` key
          - `target_userid`: TUP (primary, uid) user ID or unambiguous substring thereof in `target` key
          - `hashed`: list of hashed signature subpacket (key, value) tuples
          - `unhashed`: list of unhashed signature subpacket (key, value) tuples
          - `use_key`: *optional* string hex key ID for specifying which signing
            subkey to use (default signer is the primary key)
          - `detach`: boolean, set to True to return a lone signature packet
            instance (default False)
  
      :Returns: `openpgp.sap.msg.SignedMsg.SignedMsg` or
          `openpgp.sap.pkt.Signature.Signature` instance, depending on whether or
          not the `detach` option was set
  
      A message (`target`) is signed and returned as a signed message. If the
      intent is to apply a signature to a public key message component (ex: a
      direct signature on a key or a certification binding a user ID to the
      primary key), send the key as the `target` and use either `target_key` or
      `userid` to specify which component to sign.
  
      In every case, the resulting signature packet (alone) can be retrieved
      using the optional keyword `detach`.
  
      Signature types and appropriate values
      --------------------------------------
      ``SIG_BINARY`` & ``SIG_TEXT`` sign a literal message or detatched string.
  
      Signatures on public key block leaders (primary signing keys, subkeys, user
      IDs and user attributes) require a public key message and appropriate
      target. These signatures include ``SIG_GENERIC``, ``SIG_PERSONA``,
      ``SIG_CASUAL``, ``SIG_POSITIVE``, ``SIG_SUBKEYBIND``, ``SIG_SUBKEYREVOC``,
      ``SIG_DIRECT``, and ``SIG_KEYREVOC``.
  
      **The following signature types are not implemented:** ``SIG_CERTREVOC``,
      ``SIG_STANDALONE``, ``SIG_TIMESTAMP``, and ``SIG_THIRDPARTY``.
  
      Signature subpacket values
      --------------------------
      - ``SIGSUB_CREATED``: (0x02, t) - time at which the signature was created,
        0 <= t <= 4294967295
      - ``SIGSUB_EXPIRES``: (0x03, t) - number of seconds after creation time for
        which signature remains valid, 0 <= t
      - ``SIGSUB_REVOCABLE``: (0x07, b) - whether or not the signature may be
        revoked, b is either True or False
      - ``SIGSUB_KEYEXPIRES``: (0x09, t) - number of seconds after creation that
        the applicable key is valid, 0 <= t
      - ``SIGSUB_REVOKER``: (0x0C, (c, a, f) - which key may revoke the
        signature, c:0x80 (for now), a:public key algorithm code, and
        f:40-character key fingerprint
      - ``SIGSUB_SIGNERID``: (0x10, i) - signing key's ID, i:16 octets key ID of
        signing key
      - ``SIGSUB_NOTE``: (0x14, (f, n, v)) - a notation on the signature (open to
        interpretation), n:notation name string, v:notation value string, f:list
        of notation flags (see rfc2440 5.2.3.16 for details) or None for default
        text notations (see `pkt.Signature.create_SignatureSubpacket`)
      - ``SIGSUB_POLICYURL``: (0x1A, p) - p:string URL containing signature
        policy information
      - ``SIGSUB_SIGNERUID``: (0x1C, i) - i:user ID string identifying the
        signing key
      - ``SIGSUB_REVOCREASON``: (0x1D, (c, r)) - c:reason code, r:str reason (see
        RFC 2440 5.2.3.23 for details)
      - ``SIGSUB_SIGTARGET``: (0x1F, (k, h, s)) - target this signature should be
        applied to (used only for signature types 0x30, 0x40, and 0x50), k:public
        key algorithm code, h:hash algorithm code, and s:hash of target signature packet
  
      'Hashed' subpacket information is protected (verifiable) by the signature
      itself while 'unhashed' subpackets remain unprotected (unverifiable).
  
      :note: Signature types and the contexts they are forced into are not
          necessarily regulated (for example, it may be possible to create an
          inappropriate type of signature for a given target). Errors may be
          noticed down the line, most likely during the creation of the signature
          hash. In any case, it's best to plan ahead.
      :note: The status of the private signing key may not be available within
          the signing key message (ex. a private signing subkey may not have a
          signature packet binding it to the primary). It is up to you the caller
          to handle this.
docstring_summary: Create a signed message or detached signature instance.
docstring_tokens: Create a signed message or detached signature instance .
function: def sign_msg(sigtype, key, **kw):
      """Create a signed message or detached signature instance.
  
      :Parameters:
          - `sigtype`: integer signature type constant
          - `key`: signing key message `openpgp.sap.msg.KeyMsg.SecretKeyMsg`
            instance
  
      :Keywords:
          - `passphrase`: string passphrase for secret signing key
          - `target`: message to sign
          - `target_key`: TUP (primary, keyid) key ID or fingerprint of key packet in `target` key
          - `target_userid`: TUP (primary, uid) user ID or unambiguous substring thereof in `target` key
          - `hashed`: list of hashed signature subpacket (key, value) tuples
          - `unhashed`: list of unhashed signature subpacket (key, value) tuples
          - `use_key`: *optional* string hex key ID for specifying which signing
            subkey to use (default signer is the primary key)
          - `detach`: boolean, set to True to return a lone signature packet
            instance (default False)
  
      :Returns: `openpgp.sap.msg.SignedMsg.SignedMsg` or
          `openpgp.sap.pkt.Signature.Signature` instance, depending on whether or
          not the `detach` option was set
  
      A message (`target`) is signed and returned as a signed message. If the
      intent is to apply a signature to a public key message component (ex: a
      direct signature on a key or a certification binding a user ID to the
      primary key), send the key as the `target` and use either `target_key` or
      `userid` to specify which component to sign.
  
      In every case, the resulting signature packet (alone) can be retrieved
      using the optional keyword `detach`.
  
      Signature types and appropriate values
      --------------------------------------
      ``SIG_BINARY`` & ``SIG_TEXT`` sign a literal message or detatched string.
  
      Signatures on public key block leaders (primary signing keys, subkeys, user
      IDs and user attributes) require a public key message and appropriate
      target. These signatures include ``SIG_GENERIC``, ``SIG_PERSONA``,
      ``SIG_CASUAL``, ``SIG_POSITIVE``, ``SIG_SUBKEYBIND``, ``SIG_SUBKEYREVOC``,
      ``SIG_DIRECT``, and ``SIG_KEYREVOC``.
  
      **The following signature types are not implemented:** ``SIG_CERTREVOC``,
      ``SIG_STANDALONE``, ``SIG_TIMESTAMP``, and ``SIG_THIRDPARTY``.
  
      Signature subpacket values
      --------------------------
      - ``SIGSUB_CREATED``: (0x02, t) - time at which the signature was created,
        0 <= t <= 4294967295
      - ``SIGSUB_EXPIRES``: (0x03, t) - number of seconds after creation time for
        which signature remains valid, 0 <= t
      - ``SIGSUB_REVOCABLE``: (0x07, b) - whether or not the signature may be
        revoked, b is either True or False
      - ``SIGSUB_KEYEXPIRES``: (0x09, t) - number of seconds after creation that
        the applicable key is valid, 0 <= t
      - ``SIGSUB_REVOKER``: (0x0C, (c, a, f) - which key may revoke the
        signature, c:0x80 (for now), a:public key algorithm code, and
        f:40-character key fingerprint
      - ``SIGSUB_SIGNERID``: (0x10, i) - signing key's ID, i:16 octets key ID of
        signing key
      - ``SIGSUB_NOTE``: (0x14, (f, n, v)) - a notation on the signature (open to
        interpretation), n:notation name string, v:notation value string, f:list
        of notation flags (see rfc2440 5.2.3.16 for details) or None for default
        text notations (see `pkt.Signature.create_SignatureSubpacket`)
      - ``SIGSUB_POLICYURL``: (0x1A, p) - p:string URL containing signature
        policy information
      - ``SIGSUB_SIGNERUID``: (0x1C, i) - i:user ID string identifying the
        signing key
      - ``SIGSUB_REVOCREASON``: (0x1D, (c, r)) - c:reason code, r:str reason (see
        RFC 2440 5.2.3.23 for details)
      - ``SIGSUB_SIGTARGET``: (0x1F, (k, h, s)) - target this signature should be
        applied to (used only for signature types 0x30, 0x40, and 0x50), k:public
        key algorithm code, h:hash algorithm code, and s:hash of target signature packet
  
      'Hashed' subpacket information is protected (verifiable) by the signature
      itself while 'unhashed' subpackets remain unprotected (unverifiable).
  
      :note: Signature types and the contexts they are forced into are not
          necessarily regulated (for example, it may be possible to create an
          inappropriate type of signature for a given target). Errors may be
          noticed down the line, most likely during the creation of the signature
          hash. In any case, it's best to plan ahead.
      :note: The status of the private signing key may not be available within
          the signing key message (ex. a private signing subkey may not have a
          signature packet binding it to the primary). It is up to you the caller
          to handle this.
      """
      saplog = logging.getLogger('saplog')
      msg = kw.get('target')
      keyid = kw.get('use_key', key.primary_id)
      opts = {'passphrase':kw.get('passphrase')}
      hashed = kw.get('hashed') or []
      pending_unhashed = kw.get('unhashed', [])
      # get rid of certain overlapping subpacket types
      nonex = [0x14, 0x1c] # may exist in hashed and unhashed
      hashed_types = [x[0] for x in hashed] # subpacket x: (type, value)
      unhashed = []
  
      for p in pending_unhashed:
  
          if p[0] in hashed_types and p[0] in nonex:
              pass
          else:
              unhashed.append(p)
  
      # pack up subpacket instances
      opts['hashed_subpkts'] = [create_SigSub(x[0], x[1]) for x in hashed]
      opts['unhashed_subpkts'] = [create_SigSub(x[0], x[1]) for x in unhashed]
  
      if MSG_LITERAL == msg.type:
          s = ''.join([p.body.data for p in msg.literals])
          keypkt = key.get_keypkt(keyid)
          sigpkt = CRYPT.sign(sigtype, s, keypkt, **opts)
          # create one-pass packet
          op_opts = {'sigtype':sigtype,
                    'alg_hash':HASH_SHA1, # no MUSTs involved, take it easy
                    'alg_pubkey':key._b_primary.leader.body.alg,
                    'keyid':keyid,
                    'nest':1} # fix at 1 since not handling parallel sigs
          onepassbody = create_OnePassSignatureBody(op_opts)
          onepasspkt = create_Packet(PKT_ONEPASS, onepassbody._d)
          sigmsg = list_msgs([onepasspkt] + msg._seq + [sigpkt])[0]
  
      elif msg.type in MSG_KEYS:
  
          if 'target_userid' in kw:
              keytarget = kw['target_userid']
              blocktype = 'userid'
  
          elif 'target_key' in kw:
              keytarget = kw['target_key']
              blocktype = 'key'
  
          else:
              keytarget = msg.primary_fprint
              blocktype = 'key'
  
          block = msg.get_block(blocktype, keytarget)
  
          if block:
              keypkt = key.get_keypkt(keyid)
              opts['primary'] = msg._b_primary.leader
              sigpkt = CRYPT.sign(sigtype, block.leader, keypkt, **opts)
              block.add_sig(sigpkt) # now that the block has its new sigpkt..
              sigmsg = msg # ..the keymsg is the signed msg
  
          else:
              raise PGPError("Signature target was not found.")
  
      else:
          raise NotImplementedError("Unsupported message type->(%s)." % msg.type)
  
      if kw.get('detach'): # a lot of overhead just to end up with sigpkt
          return sigpkt
      else:
          return sigmsg
function_tokens: def sign_msg ( sigtype , key , * * kw ) : saplog = logging . getLogger ( 'saplog' ) msg = kw . get ( 'target' ) keyid = kw . get ( 'use_key' , key . primary_id ) opts = { 'passphrase' : kw . get ( 'passphrase' ) } hashed = kw . get ( 'hashed' ) or [ ] pending_unhashed = kw . get ( 'unhashed' , [ ] ) # get rid of certain overlapping subpacket types nonex = [ 0x14 , 0x1c ] # may exist in hashed and unhashed hashed_types = [ x [ 0 ] for x in hashed ] # subpacket x: (type, value) unhashed = [ ] for p in pending_unhashed : if p [ 0 ] in hashed_types and p [ 0 ] in nonex : pass else : unhashed . append ( p ) # pack up subpacket instances opts [ 'hashed_subpkts' ] = [ create_SigSub ( x [ 0 ] , x [ 1 ] ) for x in hashed ] opts [ 'unhashed_subpkts' ] = [ create_SigSub ( x [ 0 ] , x [ 1 ] ) for x in unhashed ] if MSG_LITERAL == msg . type : s = '' . join ( [ p . body . data for p in msg . literals ] ) keypkt = key . get_keypkt ( keyid ) sigpkt = CRYPT . sign ( sigtype , s , keypkt , * * opts ) # create one-pass packet op_opts = { 'sigtype' : sigtype , 'alg_hash' : HASH_SHA1 , # no MUSTs involved, take it easy 'alg_pubkey' : key . _b_primary . leader . body . alg , 'keyid' : keyid , 'nest' : 1 } # fix at 1 since not handling parallel sigs onepassbody = create_OnePassSignatureBody ( op_opts ) onepasspkt = create_Packet ( PKT_ONEPASS , onepassbody . _d ) sigmsg = list_msgs ( [ onepasspkt ] + msg . _seq + [ sigpkt ] ) [ 0 ] elif msg . type in MSG_KEYS : if 'target_userid' in kw : keytarget = kw [ 'target_userid' ] blocktype = 'userid' elif 'target_key' in kw : keytarget = kw [ 'target_key' ] blocktype = 'key' else : keytarget = msg . primary_fprint blocktype = 'key' block = msg . get_block ( blocktype , keytarget ) if block : keypkt = key . get_keypkt ( keyid ) opts [ 'primary' ] = msg . _b_primary . leader sigpkt = CRYPT . sign ( sigtype , block . leader , keypkt , * * opts ) block . add_sig ( sigpkt ) # now that the block has its new sigpkt.. sigmsg = msg # ..the keymsg is the signed msg else : raise PGPError ( "Signature target was not found." ) else : raise NotImplementedError ( "Unsupported message type->(%s)." % msg . type ) if kw . get ( 'detach' ) : # a lot of overhead just to end up with sigpkt return sigpkt else : return sigmsg