Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 883281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:33:43+00:00 2026-05-15T12:33:43+00:00

I was wondering if someone could help me to get this method converted to

  • 0

I was wondering if someone could help me to get this method converted to ruby, is this possible at all?

public static string getSHA512(string str){
    UnicodeEncoding UE = new UnicodeEncoding();
    byte[] HashValue = null;
    byte[] MessageBytes = UE.GetBytes(str);
    System.Security.Cryptography.SHA512Managed SHhash = new System.Security.Cryptography.SHA512Managed();
    string strHex = "";
    HashValue = SHhash.ComputeHash(MessageBytes);
    foreach (byte b in HashValue){
        strHex += string.Format("{0:x2}", b);
    }
    return strHex;
}

Thanks in advance


UPDATE:

I just would like to make it clear that unfortunately it’s method is not just for SHA512 generation but a custom one. I believe that the Digest::SHA512.hexdigest would be just the SHHast instance, but if you carefully look for the method you can see that it differs a bit from a simple hash generation.
Follows the result of both functions.

# in C#
getSHA512("hello") => "5165d592a6afe59f80d07436e35bd513b3055429916400a16c1adfa499c5a8ce03a370acdd4dc787d04350473bea71ea8345748578fc63ac91f8f95b6c140b93"

# in Ruby
Digest::SHA512.hexdigest("hello") || Digest::SHA2 => "9b71d224bd62f3785d96d46ad3ea3d73319bfbc2890caadae2dff72519673ca72323c3d99ba5c11d7c7acc6e14b8c5da0c4663475c2e5c3adef46f73bcdec043" 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T12:33:43+00:00Added an answer on May 15, 2026 at 12:33 pm
    require 'digest/sha2'
    
    class String
      def sha512
        Digest::SHA2.new(512).hexdigest(encode('UTF-16LE'))
      end
    end
    
    'hello'.sha512 # => '5165d592a6afe59f80d07436e35bd…5748578fc63ac91f8f95b6c140b93'
    

    As with all my code snippets on StackOverflow, I always assume the latest version of Ruby. Here’s one that also works with Ruby 1.8:

    require 'iconv'
    require 'digest/sha2'
    
    class String
      def sha512(src_enc='UTF-8')
        Digest::SHA2.new(512).hexdigest(Iconv.conv(src_enc, 'UTF-16LE', self))
      end
    end
    
    'hello'.sha512 # => '5165d592a6afe59f80d07436e35bd…5748578fc63ac91f8f95b6c140b93'
    

    Note that in this case, you have to know and tell Ruby about the encoding the string is in explicitly. In Ruby 1.9, Ruby always knows what encoding a string is in, and will convert it accordingly, when required. I chose UTF-8 as default encoding because it is backwards-compatible with ASCII, is the standard encoding on the internet and also otherwise widely used. However, for example both .NET and Java use UTF-16LE, not UTF-8. If your string is not UTF-8 or ASCII-encoded, you will have to pass in the encoding name into the sha512 method.


    Off-Topic: 9 lines of code reduced to 1. I love Ruby!

    Well, actually that is a little bit unfair. You could have written it something like this:

    var messageBytes = new UnicodeEncoding().GetBytes(str);
    var hashValue = new System.Security.Cryptography.SHA512Managed().
        ComputeHash(messageBytes);
    return hashValue.Aggregate<byte, string>("",
        (s, b) => s += string.Format("{0:x2}", b)
    );
    

    Which is really only 3 lines (broken into 5 for StackOverflow’s layout) and most importantly gets rid of that ugly 1950s-style explicit for loop for a nice 1960s-style fold (aka. reduce aka. inject aka. Aggregate aka. inject:into: … it’s all the same).

    There’s probably an even more elegant way to write this, but a) I don’t actually know C# and .NET and b) this question is about Ruby. Focus, Jörg, focus! 🙂

    Aaand … found it:

    return string.Join("", from b in hashValue select string.Format("{0:x2}", b));
    

    I knew there had to be an equivalent to Ruby’s Enumerable#join somewhere, I just was looking in the wrong place.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was wondering if someone could help me get pointers to solve this problem.
I was wondering if someone could help me with this. I have defined my
I was wondering if someone could help me with the performance of this code
I was wondering if someone could please help me with this: printf(Enter path for
I was wondering if someone could help me with this SQL statement? Say, I
I'm wondering if someone could help guide the approach to this fairly common problem:
Was wondering if someone could help me out a little with this query: SELECT
I'm a bit new to this API, and was wondering if someone could help
I was wondering if someone could help me with this. I've retrieved the source
Wondering if someone could help me. I have next to no knowledge with Ajax,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.