diff options
author | Sam Whited <sam@samwhited.com> | 2014-11-12 15:35:44 -0500 |
---|---|---|
committer | Sam Whited <sam@samwhited.com> | 2014-11-14 18:00:12 -0500 |
commit | 0e550789d372a1a83caa432e93a4f969a0607c9a (patch) | |
tree | 126ddf717b9a3d1c4f36e904c658341fad5d5177 /src/main/java/eu/siacs/conversations/crypto/sasl/Tokenizer.java | |
parent | c61120bfc416676147998a435674bf1ac1587359 (diff) |
Add SCRAM-SHA1 support
Factor out GS2 tokanization into own class
Add authentication exception class
Fixes #71
Diffstat (limited to 'src/main/java/eu/siacs/conversations/crypto/sasl/Tokenizer.java')
-rw-r--r-- | src/main/java/eu/siacs/conversations/crypto/sasl/Tokenizer.java | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/src/main/java/eu/siacs/conversations/crypto/sasl/Tokenizer.java b/src/main/java/eu/siacs/conversations/crypto/sasl/Tokenizer.java new file mode 100644 index 00000000..4797e6e8 --- /dev/null +++ b/src/main/java/eu/siacs/conversations/crypto/sasl/Tokenizer.java @@ -0,0 +1,76 @@ +package eu.siacs.conversations.crypto.sasl; + +import android.util.Base64; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Iterator; +import java.util.List; +import java.util.NoSuchElementException; + +/** + * A tokenizer for GS2 header strings + */ +public final class Tokenizer implements Iterator<String>, Iterable<String> { + private final List<String> parts; + private int index; + + public Tokenizer(final byte[] challenge) { + final String challengeString = new String(challenge); + parts = new ArrayList<>(Arrays.asList(challengeString.split(","))); + index = 0; + } + + /** + * Returns true if there is at least one more element, false otherwise. + * + * @see #next + */ + @Override + public boolean hasNext() { + return parts.size() != index + 1; + } + + /** + * Returns the next object and advances the iterator. + * + * @return the next object. + * @throws java.util.NoSuchElementException if there are no more elements. + * @see #hasNext + */ + @Override + public String next() { + if (hasNext()) { + return parts.get(index++); + } else { + throw new NoSuchElementException("No such element. Size is: " + parts.size()); + } + } + + /** + * Removes the last object returned by {@code next} from the collection. + * This method can only be called once between each call to {@code next}. + * + * @throws UnsupportedOperationException if removing is not supported by the collection being + * iterated. + * @throws IllegalStateException if {@code next} has not been called, or {@code remove} has + * already been called after the last call to {@code next}. + */ + @Override + public void remove() { + if(index <= 0) { + throw new IllegalStateException("You can't delete an element before first next() method call"); + } + parts.remove(--index); + } + + /** + * Returns an {@link java.util.Iterator} for the elements in this object. + * + * @return An {@code Iterator} instance. + */ + @Override + public Iterator<String> iterator() { + return parts.iterator(); + } +} |