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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T19:18:33+00:00 2026-05-29T19:18:33+00:00

To protect against password brute-forcing, and to protect against timing attacks trying to detect

  • 0

To protect against password brute-forcing, and to protect against timing attacks trying to detect valid usernames, I want my login process to take a constant amount of time regardless of successful authentication or not. In short I always want my login process to take say 1000ms (assume either outcome takes fractions of that time), regardless of the outcome. What’s the best way to accomplish this in Java? This is my current thinking:

class ConstantDuration<T> implements Callable<T> {

 ConstantDuration(Callable<T> task,long duration) {
  this.task = task;
  this.duration = duration;
 }

 public T call() throws Exception {
  long start = System.currentTimeMillis();
  long elapsed = 0;
  T result = null;
  try {
   result = task.call();
  } finally {
    elapsed = System.currentTimeMillis() - start;
  }
  if ( elapsed < duration ) {
   Thread.sleep(duration-elapsed);
  }
  return result;
 }
}
  • 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-29T19:18:35+00:00Added an answer on May 29, 2026 at 7:18 pm

    Obviously, you’ll want to move sleeping into the finally block so it also occurs if task throws an Exception (for instance a PasswordExpiredException?).

    The other problem is handling the case elapsed > duration. You say that never happens, but are you sure? What if your database query gets stalled by a lock? Assuming you want to deny authentication in such an event, you could do:

    ExecutorService exec = Executors.newFixedThreadPool(10);
    
    <T> T doInConstantTime(Callable<T> task, long millis, T defaultResponse) {
        Future<T> future = exec.submit(task);
        Thread.sleep(millis);
        if (future.isDone()) {
            return future.get();
        } else {
            future.cancel(false); // or true? 
            return defaultResponse;
        }
    }
    

    (You’ll need to add proper exception handling, of course)

    I am not sure though this is a good way to defend against brute force attacks, though. Instead, we invalidate a user’s login after the third consequtive failed login attempt (for a certain period of time or until an administrator unlocks the account). Mention somewhere that you do this, and nobody has a reason to brute force passwords.

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

Sidebar

Related Questions

WCF has lots of limits imposed on it to protect against DoS attacks and
I want to protect my website against spambots with javascript. I found this code,
How does one protect against XSRF attacks in Grails. I see that forms support
I am doing the following to protect subbmited data against sql attacks $myusername =
Does this protect against SQL injection attacks? function sanitize($value) { // Stripslashes if (is_array($value))
How does Windows protect against a user-mode thread from arbitrarily transitioning the CPU to
Do Entity Framework functions automatically escape input to protect against injection? In my SQL
On our own website, it's easy to protect against direct links to our video
My aim is to protect users against of creation 105 infotype (communication) for some
I have added this fix https://gist.github.com/2382288 for protect all fields against mass assignment in

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.