epita-std
/
ACU
Archived
1
0
Fork 0
This repository has been archived on 2021-10-08. You can view files and clone it, but cannot push or open issues or pull requests.
ACU/ACU/Password.pm

56 lines
961 B
Perl

#! /usr/bin/env perl
package Password;
use v5.10.1;
use strict;
use warnings;
use MIME::Base64 qw(encode_base64 decode_base64);
our $hostname = `hostname`;
chomp($hostname);
sub cxor($$)
{
my $msg = shift;
my $key = shift;
my $xor = "";
my $lk = length($key);
my $lm = length($msg);
for (my $i = 0; $i < $lm; $i++) {
$xor .= substr($msg, $i, 1) ^ substr($key, $i % $lk, 1);
}
return $xor;
}
sub gen_password(;$$)
{
my $file = shift // ".secret_intra";
my $pass = shift // `pwgen -s -n -c -y -1 42 1`;
chomp($pass);
open SECRET, ">", $file or die $!;
print SECRET encode_base64(cxor($pass, $hostname));
close SECRET;
return $pass;
}
sub get_password(;$)
{
my $file = shift // ".secret_intra";
my $decode = "";
open SECRET, "<", $file or die $file.': '.$!;
while (<SECRET>) {
$decode .= $_;
}
close SECRET;
return cxor(decode_base64($decode), $hostname);
}
1;