Convert SHA1 to hex64 in base64 (for Apache htpasswd) in PERL

I have a PHP application in which passwords are stored in the database as the output of sha1 ($ password), which, apparently, is the hexadecimal representation of the binary SHA1 hash. (as I understand)

I would like to convert this to a format compatible with Apache.htpassword files, which must be a base64 encoded binary value or base64_encode output (sha1 ($ password, true)).

I found this topic: Convert base64'd SHA1 hashes to hashes ... which does the opposite of what I need to do and it works fine, I tried changing the order of the commands and using hex2bin instead of bin2hex, but this does not work:

Fatal error: call to undefined hex2bin () function in php shell code on line 1

Apparently this is not available until PHP 5.4, and this server is still located at 5.3.x http://php.net/manual/en/function.hex2bin.php

Here is the real problem. I really need to convert it to PERL, preferably only using standard built-in modules, so that everything is simple. I'm not sure why they use perl for this step, but I'm trying to edit one very small part of a larger application and don't want to change all this :)

To be clear, I am not trying to convert hexadecimal numbers to binary numbers. This is a hexadecimal representation of the binary value stored in perl "string" :)

Thanks in advance, Tommy

+4
source share
1 answer

You explain so much, but do not leave me up to date with what you want: /

If you want to convert from a hexadecimal string to a blob string in base64, then this is what you say you want in the top paragraph of your question,

use MIME::Base64; my $bin = pack "H*", $hex_str; my $encoded = encode_base64($bin); 

which exactly matches what you want: base64_encode (sha1 ($ password, true))

Ignore my previous answers and changes.

+6
source

All Articles