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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:21:24+00:00 2026-05-25T01:21:24+00:00

I want to implement a String comparison function that doesn’t take a different amount

  • 0

I want to implement a String comparison function that doesn’t take a different amount of time depending on the number of characters that match or the position of the first mismatch. I assume there must be a library out there somewhere that provides this, but I was unable to find it via a quick search.

So far, the best idea I’ve got is to sum the XOR of each character and return whether or not the sum is 0. However, I’m pretty sure this wouldn’t work so well with Unicode. I also have a vague concern that HotSpot would do some optimizations that would change my constant-time property, but I can’t think of a specific optimization that would do this off the top of my head.

Thanks.

UPDATE: Sorry, I don’t believe I was clear. I’m not looking for O(1), I’m looking for something that won’t leak timing information. This would be used to compare hashed password values, and if the time it took to compare was different based on where the first mismatch occurred, that would be leaking information to an attacker.

  • 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-25T01:21:25+00:00Added an answer on May 25, 2026 at 1:21 am

    I see two immediate possibilities for not leaking password-related information in timing:

    1/ Pad both the password string and candidate string out to 1K, with a known, fixed character (like A). Then run the following (pseudo-code):

    match = true
    for i = 0 to 1023:
        if password[i] != candidate[i]:
            match = false
    

    That way, you’re always taking the same amount of loops to do the comparison regardless of where it matches.

    There’s no need to muck about with xor since you can still do a simple comparison, but without exiting the loop early.

    Just set the match flag to false if a mismatch is found and keep going. Once the loop exits (taking the same time regardless of size or content of password and candidate), then check whether it matched.

    2/ Just add a large (relative to the normal comparison time) but slightly random delay at the end of the comparison. For example, a random value between 0.9 and 1.1 seconds. The time taken for the comparison should be swamped by the delay and the randomness should fully mask any information leakage (unless your randomness algorithm leaks information, of course).

    That also has the added advantage of preventing brute force attacks since a password check takes at least about a second.

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

Sidebar

Related Questions

I'm using Visual Studio 2008, and I want to implement string formatting function without
Microsoft has announce that WindowsLiveID become a OpenID provider . I want implement it
I want to implement Craig Andera's custom XML configuration handler in a slightly different
Issue I want to implement TransparencyChecker Interface which would verify that from any exchange
I want to implement a search box in a window form. In that window
I want to implement a keyed observable collection in Silverlight, that will store unique
I want to implement an aplication where I have various Objects that can be
I want to implement a method like this: public Iterator<File> getFiles(String root) { //
I want to implement a simple attribute that is used to map Database Columns
I want to implement a string class which has an option to create the

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.