Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Asgeir S. Nilsen http://www.asgeirnilsen.com/

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

Password authentication without revealing your password

The majority of personalized web sites use some kind of form-based password authentication where you have two form fields for username and password, and a login button. When you submit your authentication, the password is sent in the clear to the server for verification against a user database.

Using a Javascript SHA library and one simple onsubmit protects the password in transit and also inside the user database:

<form onsubmit="pwField.value = b64_sha256(pwField.value);">


Read this for more elaborations with increased security.

Handy Iterable for a range of Integers

Looping a specific number of times produces code a bit too verbose in Java. The JDK5 enhanced for statement is a handy improvement, but you still have to fall back to the traditional for statement if you want to repeat a loop, say, 10 times.

Not with this implementation of Iterable. You can use it like this:

for (int i : new Range(100))
    System.out.printf("I have said this %d times. Read dzone.com!%n", i);

class Range implements Iterable<Integer> {
    
    private final Integer stop;
    
    public Range(int stop) {
        this.stop = stop;
    }

    public Iterator<Integer> iterator() {
        return new Iterator<Integer>() {
            
            private Integer counter = 0;

            public boolean hasNext() {
                return (counter != stop);
            }

            public Integer next() {
                if (counter == stop)
                    throw new NoSuchElementException();
                return ++counter;
            }

            public void remove() {
            }};
    }
    
}
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS