how to convert c# code to perl or openssl cli?












-1















I have a c# code that convert a string to md5 and then base64 encode it.
I would like to do the same thing with a perl script or linux cli, openssl.
i tried using openssl cli, but i get a different result from the c# code.
can anyone explain and show me how to convert the c# code to linux cli or perl script?



Thanks.



public class Program {
public static void Main() {
var myString = "7512";
var o = CalculateMD5Hash(myString);
Console.WriteLine("Generated String is: {0}", o);

}

public static string CalculateMD5Hash(string input) {
HashAlgorithm ha = new MD5CryptoServiceProvider();
string prefix = "MD5:";
string password = "";
UnicodeEncoding enc = new UnicodeEncoding();
password = Convert.ToBase64String(ha.ComputeHash(enc.GetBytes(input)));
return prefix+password;
}
}

# Linux openssl cli
echo -ne '7512' | openssl dgst -md5 -binary | openssl base64


When i run the c# code with the string: 7512, i get:
cA5YjDeU2fOJwwnVFPCuAw==



But when i am using the openssl cli command, i get:
FhxcWtUfzIhBV4kFEbPIsA==










share|improve this question

























  • Could be an encoding issue. Did you try to use UTF-8 encoding instead of UTF-16 (UnicodeEncoding)?

    – Klaus Gütter
    Jan 1 at 9:12











  • @KlausGütter, tried to change the encoding, and the result remains the same.

    – Smaug Ori
    Jan 1 at 10:17
















-1















I have a c# code that convert a string to md5 and then base64 encode it.
I would like to do the same thing with a perl script or linux cli, openssl.
i tried using openssl cli, but i get a different result from the c# code.
can anyone explain and show me how to convert the c# code to linux cli or perl script?



Thanks.



public class Program {
public static void Main() {
var myString = "7512";
var o = CalculateMD5Hash(myString);
Console.WriteLine("Generated String is: {0}", o);

}

public static string CalculateMD5Hash(string input) {
HashAlgorithm ha = new MD5CryptoServiceProvider();
string prefix = "MD5:";
string password = "";
UnicodeEncoding enc = new UnicodeEncoding();
password = Convert.ToBase64String(ha.ComputeHash(enc.GetBytes(input)));
return prefix+password;
}
}

# Linux openssl cli
echo -ne '7512' | openssl dgst -md5 -binary | openssl base64


When i run the c# code with the string: 7512, i get:
cA5YjDeU2fOJwwnVFPCuAw==



But when i am using the openssl cli command, i get:
FhxcWtUfzIhBV4kFEbPIsA==










share|improve this question

























  • Could be an encoding issue. Did you try to use UTF-8 encoding instead of UTF-16 (UnicodeEncoding)?

    – Klaus Gütter
    Jan 1 at 9:12











  • @KlausGütter, tried to change the encoding, and the result remains the same.

    – Smaug Ori
    Jan 1 at 10:17














-1












-1








-1








I have a c# code that convert a string to md5 and then base64 encode it.
I would like to do the same thing with a perl script or linux cli, openssl.
i tried using openssl cli, but i get a different result from the c# code.
can anyone explain and show me how to convert the c# code to linux cli or perl script?



Thanks.



public class Program {
public static void Main() {
var myString = "7512";
var o = CalculateMD5Hash(myString);
Console.WriteLine("Generated String is: {0}", o);

}

public static string CalculateMD5Hash(string input) {
HashAlgorithm ha = new MD5CryptoServiceProvider();
string prefix = "MD5:";
string password = "";
UnicodeEncoding enc = new UnicodeEncoding();
password = Convert.ToBase64String(ha.ComputeHash(enc.GetBytes(input)));
return prefix+password;
}
}

# Linux openssl cli
echo -ne '7512' | openssl dgst -md5 -binary | openssl base64


When i run the c# code with the string: 7512, i get:
cA5YjDeU2fOJwwnVFPCuAw==



But when i am using the openssl cli command, i get:
FhxcWtUfzIhBV4kFEbPIsA==










share|improve this question
















I have a c# code that convert a string to md5 and then base64 encode it.
I would like to do the same thing with a perl script or linux cli, openssl.
i tried using openssl cli, but i get a different result from the c# code.
can anyone explain and show me how to convert the c# code to linux cli or perl script?



Thanks.



public class Program {
public static void Main() {
var myString = "7512";
var o = CalculateMD5Hash(myString);
Console.WriteLine("Generated String is: {0}", o);

}

public static string CalculateMD5Hash(string input) {
HashAlgorithm ha = new MD5CryptoServiceProvider();
string prefix = "MD5:";
string password = "";
UnicodeEncoding enc = new UnicodeEncoding();
password = Convert.ToBase64String(ha.ComputeHash(enc.GetBytes(input)));
return prefix+password;
}
}

# Linux openssl cli
echo -ne '7512' | openssl dgst -md5 -binary | openssl base64


When i run the c# code with the string: 7512, i get:
cA5YjDeU2fOJwwnVFPCuAw==



But when i am using the openssl cli command, i get:
FhxcWtUfzIhBV4kFEbPIsA==







c# perl






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 1 at 10:18







Smaug Ori

















asked Jan 1 at 7:14









Smaug OriSmaug Ori

91




91













  • Could be an encoding issue. Did you try to use UTF-8 encoding instead of UTF-16 (UnicodeEncoding)?

    – Klaus Gütter
    Jan 1 at 9:12











  • @KlausGütter, tried to change the encoding, and the result remains the same.

    – Smaug Ori
    Jan 1 at 10:17



















  • Could be an encoding issue. Did you try to use UTF-8 encoding instead of UTF-16 (UnicodeEncoding)?

    – Klaus Gütter
    Jan 1 at 9:12











  • @KlausGütter, tried to change the encoding, and the result remains the same.

    – Smaug Ori
    Jan 1 at 10:17

















Could be an encoding issue. Did you try to use UTF-8 encoding instead of UTF-16 (UnicodeEncoding)?

– Klaus Gütter
Jan 1 at 9:12





Could be an encoding issue. Did you try to use UTF-8 encoding instead of UTF-16 (UnicodeEncoding)?

– Klaus Gütter
Jan 1 at 9:12













@KlausGütter, tried to change the encoding, and the result remains the same.

– Smaug Ori
Jan 1 at 10:17





@KlausGütter, tried to change the encoding, and the result remains the same.

– Smaug Ori
Jan 1 at 10:17












1 Answer
1






active

oldest

votes


















0














Well, got an answer in perlmonks site.
https://perlmonks.org/index.pl?node_id=1227872



UnicodeEncoding encodes to UTF-16LE. So in Perl, you'll have to use the Encode module for that, and then either use md5_base64 from Digest::MD5, which removes the padding at the end of the string (==), or, if you want the padding, you'll have to use MIME::Base64 separately:



use warnings;
use strict;
use Encode qw/encode/;
use Digest::MD5 qw/md5/;
use MIME::Base64 qw/encode_base64/;

my $string = "7512";
my $md5b64 = encode_base64( md5( encode( 'UTF-16LE', $string,
Encode::FB_CROAK|Encode::LEAVE_SRC ) ), "" );
print "Generated String is: MD5:$md5b64n";

my $expect = "cA5YjDeU2fOJwwnVFPCuAw==";
print $md5b64 eq $expect ? "Matches!n" : "Doesn't match!n";

__END__





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53993706%2fhow-to-convert-c-sharp-code-to-perl-or-openssl-cli%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Well, got an answer in perlmonks site.
    https://perlmonks.org/index.pl?node_id=1227872



    UnicodeEncoding encodes to UTF-16LE. So in Perl, you'll have to use the Encode module for that, and then either use md5_base64 from Digest::MD5, which removes the padding at the end of the string (==), or, if you want the padding, you'll have to use MIME::Base64 separately:



    use warnings;
    use strict;
    use Encode qw/encode/;
    use Digest::MD5 qw/md5/;
    use MIME::Base64 qw/encode_base64/;

    my $string = "7512";
    my $md5b64 = encode_base64( md5( encode( 'UTF-16LE', $string,
    Encode::FB_CROAK|Encode::LEAVE_SRC ) ), "" );
    print "Generated String is: MD5:$md5b64n";

    my $expect = "cA5YjDeU2fOJwwnVFPCuAw==";
    print $md5b64 eq $expect ? "Matches!n" : "Doesn't match!n";

    __END__





    share|improve this answer




























      0














      Well, got an answer in perlmonks site.
      https://perlmonks.org/index.pl?node_id=1227872



      UnicodeEncoding encodes to UTF-16LE. So in Perl, you'll have to use the Encode module for that, and then either use md5_base64 from Digest::MD5, which removes the padding at the end of the string (==), or, if you want the padding, you'll have to use MIME::Base64 separately:



      use warnings;
      use strict;
      use Encode qw/encode/;
      use Digest::MD5 qw/md5/;
      use MIME::Base64 qw/encode_base64/;

      my $string = "7512";
      my $md5b64 = encode_base64( md5( encode( 'UTF-16LE', $string,
      Encode::FB_CROAK|Encode::LEAVE_SRC ) ), "" );
      print "Generated String is: MD5:$md5b64n";

      my $expect = "cA5YjDeU2fOJwwnVFPCuAw==";
      print $md5b64 eq $expect ? "Matches!n" : "Doesn't match!n";

      __END__





      share|improve this answer


























        0












        0








        0







        Well, got an answer in perlmonks site.
        https://perlmonks.org/index.pl?node_id=1227872



        UnicodeEncoding encodes to UTF-16LE. So in Perl, you'll have to use the Encode module for that, and then either use md5_base64 from Digest::MD5, which removes the padding at the end of the string (==), or, if you want the padding, you'll have to use MIME::Base64 separately:



        use warnings;
        use strict;
        use Encode qw/encode/;
        use Digest::MD5 qw/md5/;
        use MIME::Base64 qw/encode_base64/;

        my $string = "7512";
        my $md5b64 = encode_base64( md5( encode( 'UTF-16LE', $string,
        Encode::FB_CROAK|Encode::LEAVE_SRC ) ), "" );
        print "Generated String is: MD5:$md5b64n";

        my $expect = "cA5YjDeU2fOJwwnVFPCuAw==";
        print $md5b64 eq $expect ? "Matches!n" : "Doesn't match!n";

        __END__





        share|improve this answer













        Well, got an answer in perlmonks site.
        https://perlmonks.org/index.pl?node_id=1227872



        UnicodeEncoding encodes to UTF-16LE. So in Perl, you'll have to use the Encode module for that, and then either use md5_base64 from Digest::MD5, which removes the padding at the end of the string (==), or, if you want the padding, you'll have to use MIME::Base64 separately:



        use warnings;
        use strict;
        use Encode qw/encode/;
        use Digest::MD5 qw/md5/;
        use MIME::Base64 qw/encode_base64/;

        my $string = "7512";
        my $md5b64 = encode_base64( md5( encode( 'UTF-16LE', $string,
        Encode::FB_CROAK|Encode::LEAVE_SRC ) ), "" );
        print "Generated String is: MD5:$md5b64n";

        my $expect = "cA5YjDeU2fOJwwnVFPCuAw==";
        print $md5b64 eq $expect ? "Matches!n" : "Doesn't match!n";

        __END__






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 1 at 14:34









        Smaug OriSmaug Ori

        91




        91
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53993706%2fhow-to-convert-c-sharp-code-to-perl-or-openssl-cli%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

            How to fix TextFormField cause rebuild widget in Flutter