-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement detection of non-UTF8 passwords
- Loading branch information
1 parent
3e1502f
commit 8877bae
Showing
4 changed files
with
80 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// SPDX-FileCopyrightText: 2022 Paul Schaub <vanitasvitae@fsfe.org> | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package sop.util; | ||
|
||
import sop.exception.SOPGPException; | ||
|
||
import java.nio.ByteBuffer; | ||
import java.nio.CharBuffer; | ||
import java.nio.charset.CharacterCodingException; | ||
import java.nio.charset.Charset; | ||
import java.nio.charset.CharsetDecoder; | ||
import java.nio.charset.CodingErrorAction; | ||
|
||
public class UTF8Util { | ||
|
||
private static final CharsetDecoder UTF8Decoder = Charset.forName("UTF8") | ||
.newDecoder() | ||
.onUnmappableCharacter(CodingErrorAction.REPORT) | ||
.onMalformedInput(CodingErrorAction.REPORT); | ||
|
||
/** | ||
* Detect non-valid UTF8 data. | ||
* | ||
* @see <a href="https://stackoverflow.com/a/1471193">ante on StackOverflow</a> | ||
* @param data | ||
* @return | ||
*/ | ||
public static String decodeUTF8(byte[] data) { | ||
ByteBuffer byteBuffer = ByteBuffer.wrap(data); | ||
try { | ||
CharBuffer charBuffer = UTF8Decoder.decode(byteBuffer); | ||
return charBuffer.toString(); | ||
} catch (CharacterCodingException e) { | ||
throw new SOPGPException.PasswordNotHumanReadable(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// SPDX-FileCopyrightText: 2022 Paul Schaub <vanitasvitae@fsfe.org> | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package sop.util; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import sop.exception.SOPGPException; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
public class UTF8UtilTest { | ||
|
||
@Test | ||
public void testValidUtf8Decoding() { | ||
String utf8String = "Hello, World\n"; | ||
String decoded = UTF8Util.decodeUTF8(utf8String.getBytes(StandardCharsets.UTF_8)); | ||
|
||
assertEquals(utf8String, decoded); | ||
} | ||
|
||
/** | ||
* Test detection of non-uft8 data. | ||
* @see <a href="https://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt"> | ||
* Markus Kuhn's UTF8 decoder capability and stress test file</a> | ||
*/ | ||
@Test | ||
public void testInvalidUtf8StringThrows() { | ||
assertThrows(SOPGPException.PasswordNotHumanReadable.class, | ||
() -> UTF8Util.decodeUTF8(new byte[] {(byte) 0xa0, (byte) 0xa1})); | ||
assertThrows(SOPGPException.PasswordNotHumanReadable.class, | ||
() -> UTF8Util.decodeUTF8(new byte[] {(byte) 0xc0, (byte) 0xaf})); | ||
assertThrows(SOPGPException.PasswordNotHumanReadable.class, | ||
() -> UTF8Util.decodeUTF8(new byte[] {(byte) 0x80, (byte) 0xbf})); | ||
} | ||
} |