Skip to content

Instantly share code, notes, and snippets.

@kost
Last active December 15, 2015 01:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kost/5177541 to your computer and use it in GitHub Desktop.
Save kost/5177541 to your computer and use it in GitHub Desktop.
Cisco Password Type 4 - SHA256 implementation in Perl
#!/usr/bin/perl
# Basic Cisco type 4 - password encoder by Kost
# Base64 custom encoder taken from VOMS::Lite::Base64
# Example: ./cisco-4-sha256.pl password
use strict;
use Digest::SHA qw(sha256);
my %Alphabets = (
CISCO => "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
);
sub Encode{
my $data = shift;
my $str = shift; # Can supply custom Base64
my $pad="";
if ( defined $str ) {
$str = $Alphabets{$str} if ($Alphabets{$str});
if ( $str =~ /^(.{64})(.?)$/s ) { $str=$1; $pad="$2"; }
else { return undef; }
}
else { $str = $Alphabets{'CISCO'}; }
$data=~s|(.)(.?)(.?)| substr($str,((ord($1)&252)>>2),1).
substr($str,((ord($1)&3)<<4)+((ord($2)&240)>>4),1).
((length($2))?substr($str,((ord($2)&15)<<2)+((ord($3)&192)>>6),1):$pad).
((length($3))?substr($str,(ord($3)&63),1):$pad)|gse;
return $data;
}
sub cisco256 {
my $data=shift;
my $ret = Encode(sha256($data),"CISCO");
return $ret;
}
my $pass = shift;
print cisco256($pass)."\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment