1

I've looked everywhere and I can't seem to find the escape sequence for a space. I'm currently using " " for space but it's not working. I need it for my hash function which fails to compute properly when it hashes a password that includes a space character.

For example when I input "aaa aaa" into my program, it outputs ">&q". My function should output a seven character hash based on the password, but it stops at the space instead, leaving only a three character output. However, the function can still output a space given certain inputs.

import java.util.*;

public class Hasher {

    private static Scanner scan;

    /*
     * This function will generate a hash based off a password entered by the
     * user
     */
    public static void main(String[] args) {

        scan = new Scanner(System.in);

        System.out.println("What is your password?");

        String password = scan.next();

        String characters = "qwertyuiopasdfghjklzxcvbnm";
        characters = characters + characters.toUpperCase();
        characters = characters + "1234567890";
        characters = characters + " ";
        characters = characters + "!@#$%^&*()_+-=`~\b[]?-{};',./:\"<>?\\";

        char[] array = new char[2 * characters.length()];

        for (int y = 0; y < array.length; y++) {

            Random rand = new Random(y);
            array[y] = characters.charAt(rand.nextInt(characters.length()));
        }

        String newPass = "";

        for (int y = 0; y < password.length(); y++) {

            char x = password.charAt(y);

            for (int z = 0; z < characters.length(); z++) {

                if (x == characters.charAt(z)) {

                    Random rand = new Random(y);
                    x = array[z + rand.nextInt(array.length - z)];
                    newPass = newPass + x;
                    break;
                }
            }
        }

        System.out.println("Your hash is: " + newPass);

    }

}
2
  • The various answers provide good solutions. I'm going to assume that this is for a school assignment. If you are actually planning on using this in a production assignment, Don't! Rolling your own hash functions is a bad idea, use an established function like PBKDF2.
    – Aurand
    Jan 8, 2013 at 23:52
  • TYVM all. Haha dw I'm not using this for any practical application especially after I post it on a public forum. It's just as study for AP tests in May. Jan 8, 2013 at 23:55

3 Answers 3

4

When you construct your Scanner object, pass in the delimiter as "\\n" so that it scans the entire line.

Something like: new Scanner(System.in).useDelimiter("\\n");

0
3

Just change

String password = scan.next();

to

String password = scan.nextLine();
1

This

  String password = scan.next();

only reads the first word. http://docs.oracle.com/javase/6/docs/api/java/util/Scanner.html#useDelimiter(java.lang.String)

Set the delimiter to the new line \n character.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.