概要

  • ファイルから平文を読み込んでSHA1を計算する。

ソース

sha1.pl

# SHA1 の計算

use strict;
use warnings;
use utf8;
use Encode;
use Digest::SHA1 qw( sha1_hex );

my $charsetConsole	= 'CP932';
my $charsetFile		= 'utf-8';

binmode( STDIN,  ":encoding($charsetConsole)" );
binmode( STDOUT, ":encoding($charsetConsole)" );
binmode( STDERR, ":encoding($charsetConsole)" );

my $srcFile = "./Password_Org.txt";
my $dstFile = "./Password_Enc.txt";

open( my $fhin, "<:utf8", encode( $charsetConsole, $srcFile ) ) 
	or die( "$srcFile: $!\n" );
<$fhin>;	# ヘッダ行を捨てる
my @Body = <$fhin>;
close( $fhin );
chomp( @Body );

open( my $fhout, ">:utf8", encode( $charsetConsole, $dstFile ) ) 
	or die( "$dstFile: $!\n" );
print $fhout "ID\tPlain\tEncrypted\n";

foreach my $row ( @Body ){
	my @fields = split( /\t/, $row );
	printf $fhout ( "%s\t%s\n", $row, sha1_hex( encode( 'utf8', $fields[1] ) ) );
}

close( $fhout );

# EOF

計算サンプル

入力 Password_Org.txt

ID	Plain
1	0000
2	1111
3	2222
4	3333
5	あいう

出力 Password_Enc.txt

ID	Plain	Encrypted
1	0000	39dfa55283318d31afe5a3ff4a0e3253e2045e43
2	1111	011c945f30ce2cbafc452f39840f025693339c42
3	2222	fea7f657f56a2a448da7d4b535ee5e279caf3d9a
4	3333	f56d6351aa71cff0debea014d13525e42036187a
5	あいう	eb636ba7c320e00b3749ad404b7adc7609560dee

SHA256 ハッシュの計算

perl

perl -MDigest::SHA -e 'my $sha = Digest::SHA->new(256); $sha->addfile("filename"); print $sha->b64digest . "\n";'

OpenSSL

openssl sha256 -binary "filename" | openssl base64

リンク