java.lang.Object
net.i2p.sam.SAMUtils

class SAMUtilsextends Object
Protocol helpers for SAM: destination/key parsing, naming lookups, SAM parameter parsing, and random destination generation.
  • Field Details

    • _log

      private static final Log _log
      Logger for this class.
    • COMMAND

      public static final String COMMAND
      The key for the command portion of a parsed SAM message.
      See Also:
    • OPCODE

      public static final String OPCODE
      The key for the opcode portion of a parsed SAM message.
      See Also:
  • Constructor Details

    • SAMUtils

      SAMUtils()
      The parsers and key helpers below are static, and so is the logger, so an instance carries nothing.
  • Method Details

    • genRandomKey

      public static void genRandomKey(OutputStream priv, OutputStream pub)
      Generate a random destination key using DSA_SHA1 signature type. Caller must close streams. Fails silently.
      Parameters:
      priv - Stream used to write the destination and private keys
      pub - Stream used to write the destination (may be null)
    • genRandomKey

      public static void genRandomKey(OutputStream priv, OutputStream pub, SigType sigType)
      Generate a random destination key. Caller must close streams. Fails silently.
      Parameters:
      priv - Stream used to write the destination and private keys
      pub - Stream used to write the destination (may be null)
      sigType - what signature type
    • checkPrivateDestination

      public static boolean checkPrivateDestination(String dest)
      Check whether a base64-encoded {dest,privkey,signingprivkey[,offlinesig]} is valid

      This only checks that the length is correct. It does not validate for pubkey/privkey match, or check the signatures.

      Parameters:
      dest - The base64-encoded destination and keys to be checked (same format as PrivateKeyFile)
      Returns:
      true if valid
    • lookupHost

      private static Destination lookupHost(String name)
      Resolved the specified hostname.
      Parameters:
      name - Hostname to be resolved
      Returns:
      the Destination for the specified hostname, or null if not found
    • getDest

      public static Destination getDest(String s) throws DataFormatException
      Resolve the destination from a key or a hostname.
      Parameters:
      s - Hostname or key to be resolved
      Returns:
      the Destination for the specified hostname, non-null
      Throws:
      DataFormatException - on bad Base64 or name not found
    • parseParams

      public static Properties parseParams(String args) throws SAMException
      Parse SAM parameters, and put them into a Properties object

      Modified from EepGet. COMMAND and OPCODE are mapped to upper case; keys, values, and ping data are not. Double quotes around values are stripped.

      Possible input:

       COMMAND
       COMMAND OPCODE
       COMMAND OPCODE [key=val]...
       COMMAND OPCODE [key=" val with spaces "]...
       PING
       PONG
       PING any   thing goes
       PONG any   thing   goes
      
      Escaping is allowed with a backslash, e.g. \"
       No spaces before or after '=' allowed
       Keys may not be quoted
       COMMAND, OPCODE, and keys may not have '=' or whitespace unless escaped
       Duplicate keys not allowed
      

      A key without a value is not allowed by the spec, but is returned with the value "true".

      COMMAND is returned as the value of the key ""COMMAND"". OPCODE, or the remainder of the PING/PONG line if any, is returned as the value of the key ""OPCODE"".

      Parameters:
      args - non-null
      Returns:
      non-null, may be empty. Does not throw on missing COMMAND or OPCODE; caller must check.
      Throws:
      SAMException - on some errors but not all