Skip to content

Instantly share code, notes, and snippets.

@carlosmcevilly
Last active October 10, 2015 20:38
Show Gist options
  • Save carlosmcevilly/3747652 to your computer and use it in GitHub Desktop.
Save carlosmcevilly/3747652 to your computer and use it in GitHub Desktop.
get the best matching value from an encrypted store keyed by one or more tags, and put it in the pasteboard
#!/bin/bash
export key=$1
export data=$2
export __KEYSTORE__=$3
if [[ "$data" == '' ]]; then
echo usage: $0 '<"key text"> <"data text"> [keystore_name]'
echo
echo " for multi-line data, put in file and do:"
echo
echo " add \"key1 key2\" \"\`cat 'file'\`\""
echo
echo " the quotes are important. The single quotes around 'file'"
echo " are only needed if the filename contains spaces."
echo
exit -1
fi
if [[ ! $__KEYSTORE__ ]]; then
export __KEYSTORE__=pastable
fi
if [[ "$GET_FILE_DIR" == '' ]] || [[ "$GET_PASS" == '' ]]; then
echo need to set GET_FILE_DIR and GET_PASS in environment before using this
exit -1
fi
export encrypted=`echo "$data" | openssl enc -e -aes-256-cbc -a -salt -pass env:GET_PASS | perl -p -e 's/\n//g;'`
echo "$key: [base64:$encrypted]" >> $GET_FILE_DIR/$__KEYSTORE__.txt
#!/usr/bin/perl
my @args = @ARGV;
my $dir = $ENV{'GET_FILE_DIR'};
my $pass = $ENV{'GET_PASS'};
if (!defined($dir) || $dir eq '') {
print "Before using $0, you need to set the GET_FILE_DIR environment\n";
print "variable to point to the directory where the data file resides\n";
print "(You don't have to create the file; it is created by the 'add'\n";
print "script)\n\n";
print "Optionally you can also set GET_PASS to contain the password.\n\n";
exit(-1);
}
if (!defined($pass)) {
print "GET_PASS not set, please enter password: ";
$| = 1;
$pass = <STDIN>;
chomp($pass);
$ENV{'GET_PASS'} = $pass;
}
die "usage: $0 [--keystore name] <keystring>\n\n" if (@args < 1);
my $keystore = "pastable";
if ($args[0] eq "--keystore") {
shift(@args);
$keystore = shift(@args);
}
my $in = "$dir/$keystore.txt";
open(IN, "<", $in) or die "error opening $in: $!";
my $best_score = 0;
my $best_desc = '';
my $best_answer = '';
while (my $line=<IN>) {
chomp($line);
my $score = 0;
next if ($line =~ /^\s*(#.*)?$/);
if ($line =~ /(.*):?\s*\[(.*?)\]\s*$/) {
my @keywords = split(/\s+/, $1);
my $candidate = $2;
foreach my $arg (@args) {
foreach my $keyword (@keywords) {
$keyword =~ s/:$//;
if ($arg eq $keyword) {
$score += 12; # exact match
}
elsif ($keyword =~ /^$arg$/i) {
$score += 11; # match without regard to case
}
elsif ($keyword =~ /^$arg/i) {
$score += 10; # prefix match
}
elsif ($keyword =~ /$arg/i) {
$score += 9; # embedded match
}
else {
$score && $score--;
}
}
if ($score > $best_score) {
$best_score = $score;
$best_answer = $candidate;
$best_desc = join(' ', @keywords);
}
}
}
}
close(IN);
if ($best_score > 0) {
if ($best_answer =~ /^base64:(.*)/) {
$best_answer = decrypt($1);
}
if ( -t STDOUT ) {
# printing standard output
print "$best_desc: [$best_answer]\n";
}
else {
# output redirected to a file
print STDERR "$best_desc: [<output sent to file>]\n";
binmode(STDOUT);
print STDOUT "$best_answer";
if ($best_answer =~ m{[\p{Other}\p{Control}\p{Unassigned}]}) {
# guessing the data is not binary
print STDOUT "\n";
}
else {
print STDERR "warning, binary data not handled well yet. may be corrupted.\n";
}
}
`echo "$best_answer\\c" | pbcopy`;
}
sub decrypt {
my $data = shift;
$data =~ s/(.{64})/\1\n/gs;
my $result = `echo "$data" | openssl enc -d -aes-256-cbc -a -salt -pass env:GET_PASS`;
chomp($result);
return $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment