Add canlockcheck.pl
Signed-off-by: Thomas Hochstein <thh@thh.name>
This commit is contained in:
		
						commit
						703ebc7be6
					
				
					 1 changed files with 83 additions and 0 deletions
				
			
		
							
								
								
									
										83
									
								
								canlockcheck.pl
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										83
									
								
								canlockcheck.pl
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,83 @@
 | 
			
		|||
#! /usr/bin/perl -w
 | 
			
		||||
#
 | 
			
		||||
# canlockcheck.pl
 | 
			
		||||
#
 | 
			
		||||
# Check whether Cancel-Lock and Cancel-Key match.
 | 
			
		||||
#
 | 
			
		||||
# Copyright (c) 2023 Thomas Hochstein <thh@thh.name>
 | 
			
		||||
 | 
			
		||||
use MIME::Base64();
 | 
			
		||||
use Digest::SHA();
 | 
			
		||||
use Digest::MD5();
 | 
			
		||||
use Getopt::Long qw(GetOptions);
 | 
			
		||||
Getopt::Long::config ('bundling');
 | 
			
		||||
 | 
			
		||||
my $VERSION = "0.1";
 | 
			
		||||
 | 
			
		||||
# read commandline options ############
 | 
			
		||||
my ($OptCanLock,$OptCanKey);
 | 
			
		||||
GetOptions ('l|lock=s'  => \$OptCanLock,
 | 
			
		||||
            'k|key=s'   => \$OptCanKey,
 | 
			
		||||
            'V|version' => \&ShowVersion) or exit 1;
 | 
			
		||||
 | 
			
		||||
# subroutines #########################
 | 
			
		||||
 | 
			
		||||
### display version information and exit
 | 
			
		||||
sub ShowVersion {
 | 
			
		||||
  print "canlockcheck v$VERSION\n";
 | 
			
		||||
  print "Copyright (c) 2023 Thomas Hochstein <thh\@thh.name>\n";
 | 
			
		||||
  print "This program is free software; you may redistribute it ".
 | 
			
		||||
        "and/or modify it under the same terms as Perl itself.\n";
 | 
			
		||||
  exit;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
sub verify_cancel_key($$) {
 | 
			
		||||
  my $cancel_lock = shift;
 | 
			
		||||
  my $cancel_key  = shift;
 | 
			
		||||
 | 
			
		||||
  my %lock;
 | 
			
		||||
  for my $l(split(/\s+/, $cancel_lock)) {
 | 
			
		||||
    unless($l =~ m/^(sha512|sha256|sha1|md5):(\S+)/) {
 | 
			
		||||
      printf ("Invalid Cancel-Lock syntax '%s'\n", $l);
 | 
			
		||||
      next;
 | 
			
		||||
    }
 | 
			
		||||
    $lock{$2} = $1;
 | 
			
		||||
   }
 | 
			
		||||
 | 
			
		||||
  for my $k(split(/\s+/, $cancel_key)) {
 | 
			
		||||
    unless($k =~ m/^(sha512|sha256|sha1|md5):(\S+)/) { 
 | 
			
		||||
      printf ("Invalid Cancel-Key syntax '%s'\n", $k);
 | 
			
		||||
      next;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    my $key;
 | 
			
		||||
    if ($1 eq 'sha512') {
 | 
			
		||||
      $key = Digest::SHA::sha512($2);
 | 
			
		||||
    } elsif ($1 eq 'sha256') {
 | 
			
		||||
       $key = Digest::SHA::sha256($2);
 | 
			
		||||
    } elsif($1 eq 'sha1') {
 | 
			
		||||
       $key = Digest::SHA::sha1($2);
 | 
			
		||||
    } elsif ($1 eq 'md5') {
 | 
			
		||||
       $key = Digest::MD5::md5($2);
 | 
			
		||||
    }
 | 
			
		||||
    $key = MIME::Base64::encode_base64($key, '');
 | 
			
		||||
 | 
			
		||||
    if (exists($lock{$key})) {
 | 
			
		||||
      return sprintf("Cancel-Key %s:%s matches Cancel-Lock %s:%s.", $1, $2, $lock{$key}, $key);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
 return 'No Cancel-Key matches any Cancel-Lock.';
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
# Main program ########################
 | 
			
		||||
 | 
			
		||||
if (!$OptCanLock or !$OptCanKey) {
 | 
			
		||||
  print "canlockcheck -l <Cancel-Lock> -k <Cancel-Key>\n";
 | 
			
		||||
  exit 2;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
my $result = &verify_cancel_key($OptCanLock, $OptCanKey);
 | 
			
		||||
 | 
			
		||||
printf ("%s\n", $result);
 | 
			
		||||
exit;
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue