#!/usr/bin/perl

use Digest::MD5 qw(md5 md5_hex md5_base64);
use DBI;
use Getopt::Long;


my %options;
my $crypt = -1;
GetOptions(\%options,
           "crypt" => \$crypt,
           "decrypt" => sub { $crypt = 0 },
	   "string=s");

if ($crypt == -1 || !defined($options{'string'})) {
  print "options manquantes\n";
  exit 1;
}

my $dbh = DBI->connect('DBI:mysql:rainbow','root','') || die "Connextion impossible a la base! $DBI::errstr";
if ($crypt) {
  my $sth = $dbh->prepare("SELECT hash_hash FROM hash WHERE hash_word='".$options{'string'}."' collate latin1_bin");
  my $res = $sth->execute;
  my $row = $sth->fetchrow_hashref;
  printf("hash = %s\n",$row->{hash_hash});
}
else {
  my $sth = $dbh->prepare("SELECT hash_word FROM hash WHERE hash_hash='".$options{'string'}."'");
  my $res = $sth->execute;
  my $row = $sth->fetchrow_hashref;
  printf("word = %s\n",$row->{hash_word});    
}
