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 4240760
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T03:12:34+00:00 2026-05-21T03:12:34+00:00

(Updated due to some confusion over my question) We use this method to generate

  • 0

(Updated due to some confusion over my question) We use this method to generate a digested_password, which is saved to our database for later lookup. When we authenticate a user to our application, this method is again ran against their input and if the digested_password output matches the digested_password we saved in the database we authenticate them. I need to reproduce this function in PHP for another application that shares the database.

def self.hash_password( password, salt )
  salted_password = password.insert 4, salt
  digested_password = Digest::SHA512.hexdigest("#{salted_password}")
  return digested_password
end

I really have no experience with Ruby so please forgive my lack of understanding.

Thanks!

  • 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-21T03:12:35+00:00Added an answer on May 21, 2026 at 3:12 am

    Because you can’t "retrieve" a password from a one-way hash like SHA-512, the closest we can get is being able to generate the same hash from the same password and salt. Let’s look at the Ruby code:

    def self.hash_password( password, salt )
      salted_password = password.insert 4, salt
      digested_password = Digest::SHA512.hexdigest("#{salted_password}")
      return digested_password
    end
    

    The only thing a bit "weird" here is the call to the .insert method on the password string. It’s basically splicing the salt right into the password, starting at the fourth character index. Normally salts are simply concatenated with the password.

    We can replicate this using substr:

    function hash_password($password, $salt) {
        $salted_password = substr($password, 0, 4) . $salt . substr($password, 4);
        return hash('sha512', $salted_password);
    }
    

    I’m using the now-default hash extension here, as it’s pretty much the most reliable way to generate a SHA-512 hash.

    Using this code, you should be able to generate identical hashes for a given identical password and salt combination. I’m not sure what the behavior would be when the password is less than five characters long.


    Yup, looks like it should work:

    [charles@lobotomy ~]$ irb
    irb(main):001:0> require 'digest/sha2'
    => true
    irb(main):002:0> def hash_password( password, salt )
    irb(main):003:1>   salted_password = password.insert 4, salt
    irb(main):004:1>   digested_password = Digest::SHA512.hexdigest("#{salted_password}")
    irb(main):005:1>   return digested_password
    irb(main):006:1> end
    => nil
    irb(main):007:0* puts hash_password('password', 'salt')
    92d3efdbf51d199b0930c427b77dc8d5cf41ac58b6fab5f89cc3f32d719a8f6ffcdff6211bdd0565a6e7b09925839e5dcce1fa5abf65eca87c6a883ab0b510b9
    => nil
    irb(main):018:0> exit
    [charles@lobotomy ~]$ 
    [charles@lobotomy ~]$ php -a
    Interactive shell
    
    php > function hash_password($password, $salt) {
    php {     $salted_password = substr($password, 0, 4) . $salt . substr($password, 4);
    php {     return hash('sha512', $salted_password);
    php { }
    php > echo hash_password('password', 'salt');
    92d3efdbf51d199b0930c427b77dc8d5cf41ac58b6fab5f89cc3f32d719a8f6ffcdff6211bdd0565a6e7b09925839e5dcce1fa5abf65eca87c6a883ab0b510b9
    php >
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is the updated question: the current query is doing something like: $sql1 =
I would like to update my SQL lite database with the native update-method of
newbie here, sorry if this is an obvious question, and sorry for my English.
I have an activity which has some text views and other data to be
( Updated a little ) I'm not very experienced with internationalization using PHP, it
Just updated build server with rc0 and surprise no chiron. Wondering how you package
We've just updated ASP.NET from Preview 3 to Preview 5 and we've run into
I've updated php.ini and moved php_mysql.dll as explained in steps 6 and 8 here.
I've updated my TortoiseSVN client and now I'm getting the error when trying to
I've just updated my ruby installation on my gentoo server to ruby 1.8.6 patchlevel

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.