Endpoint Encryption

 View Only

Grabbing a UUID from PGP Command Line Output

  • 1.  Grabbing a UUID from PGP Command Line Output

    Posted Apr 08, 2011 05:29 AM
      |   view attached

    Certain PGP Command Line operations together with PGP Key Managament Server will output a UUID that you will probably need for some of the next operations.

    Fo example when working with MEK Series, Certificates, Managed Secure Data there is often the need to perfom multiple operations on one object. (1. create key, 2. create a MEK Series for this key, 3. create a symmetric key on this MEK Series, etc).

    Another task where you might need to use the UUID of a PGP Command Line output and want to feed it to other PGP Command Line calls is when searching for keys on Universal Server.

    The magic regular expression that matches on all UUIDs generated by PGP Key Management Server is

     ([a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12})
    

    you should always use this regex in case insensitive matches by adding an 'i' option to the match. e.g.

     $output =~ /([a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12})/i
    

    Searching for a UUID in a string

    An example in Perl to parse a string for a UUID and return the first UUID found is:

     # The function getUUIDFromOutput will take a string as input and will return the first UUID found in this string
    # if no UUID is found, then it will return 0.
    sub getUUIDFromOutput{
        my $output = shift;
        # perform a regex match on the output string, in multiline mode, not case sensitive
        if ($output =~ /([a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12})/im){
            # if a match was found, return the first match
            return $1;
        } else {
            # If no match was found, return 0
            return 0;
        }
    }
    

    Searching for multiple UUIDs

    To search for multiple UUIDs in a string (e.g. the output of a search command) use the following function:

     # The function getUUIDArrayFromOutput will take a string as argument and will return all UUIDs found in this string as
    # a perl array. if only one UUID is found, it will return an array with only one entry. If no UUID is found it will return 0.
    sub getUUIDArrayFromOutput{
        my $output = shift;
        my @result;
        # perform a regex match on the output string, in multiline mode, not case sensitive, globally
        while ($output =~ /([a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12})/isg)
        {
            # if a match was found, push the result on top of the array
            push(@result,$1);
        }
        # now if the array is empty still, return 0
        if ($#result < 0){
            # If no match was found, return 0
            return 0;
        } else {
            # Else return the array
            return @result;
        }
    }
    

    Example script

    Attached is a working perl script that uses these functions and runs through a couple of examples

    Attachment(s)

    txt
    uuid-example_pl.txt   4 KB 1 version