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

  • Home
  • SEARCH
  • 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 9020961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:08:42+00:00 2026-06-16T05:08:42+00:00

I am having a problem with the session in play framework 1.2.4. When I

  • 0

I am having a problem with the session in play framework 1.2.4. When I add a SHA256-hash of a particular String (“testDude5”) to the session and retrieve it afterwards, the values are not the same. It doesn’t happen with other Strings like “testDude1”. Here is the sample code to reproduce the result.

package controllers;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import play.mvc.Controller;


public class ExampleController extends Controller
{

    public static final String test1 = "testDude1";
    public static final String test2 = "testDude5";

    public static void set()
    {
        session.put("test1", getHash(test1));
        session.put("test2", getHash(test2));
    }

    public static void get()
    {
        String output = "";

        output += "Test 1 compare: ";
        output += session.get("test1").equals(getHash(test1)) ? "success" : "failed";
        output += "\n";

        output += "Test 2 compare: ";
        output += session.get("test2").equals(getHash(test2)) ? "success" : "failed";
        output += "\n";

        renderText(output);
    }

    /**
     * Generates the hash value for a password.
     * 
     * @param password
     * @return hash
     */
    public static String getHash(String password)
    {
        // Create an digest object
        MessageDigest md;

        try
        {
            // Try to get sha-265
            md = MessageDigest.getInstance("SHA-256");

            // Encrypt the password
            md.update(password.getBytes("UTF-8"));

            // Get the encrypted password
            byte[] digest = md.digest();

            // Convert byte array to String
            String str = new String(digest);

            // Return encrypted password
            return str;
        }
        catch (NoSuchAlgorithmException e)
        {
            e.printStackTrace();
        }
        catch (UnsupportedEncodingException e)
        {
            e.printStackTrace();
        }

        return null;
    }

}

I am totally puzzled by this. Does anyone have an idea whats going on there. Thanks for any advice.

Cheers

  • 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-06-16T05:08:43+00:00Added an answer on June 16, 2026 at 5:08 am

    The problem is in your getHash function. There is nothing wrong with play framework session.

    public static String getHash(String password) {
        ....
        // Get the encrypted password
        byte[] digest = md.digest();
        // Convert byte array to String
        String str = new String(digest); // DON'T do this with digest! 
        // The behavior is unspecified.
    

    According to Java API doc, this constructor “Constructs a new String by decoding the specified array of bytes using the platform’s default charset…The behavior of this constructor when the given bytes are not valid in the default charset is unspecified“. However your hash digest may contains something not valid in the default charset.

    Play framework provides a nice utility function Codec.byteToHeString() to transform digest in byte[] into hex string. This might be just what you need.

        // Codec resides in play.libs
        String str = Codec.byteToHexString(digest);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Having problem with the middle Div not expanding to the width http://acs.graphicsmayhem.com/images/middiv.jpg Ok, how
I am having an problem writing unit tests for an simple session wrapper. The
I am having this problem.. using (var transaction = Session.BeginTransaction()) { var t =
I'm having a problem of losing session variable on my website. It append at
I am having a problem with using session variables. I have two controllers named
so i am having a problem passing php session variables to multiple pages. Essentially
I am having a problem with a Many-to-Many association and the session in nHibernate
I'm teaching myself Zend am and having a problem with using my session to
I am having problem with a session variable. The problem is that with a
I'm having a problem with Symfony creating a new session on each page load,

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.