mirror of
https://codeberg.org/PGPainless/sop-java.git
synced 2024-11-25 16:42:07 +01:00
UTF8Util.decodeUTF8(): throw CharacterCodingException instead of PasswordNotHumanReadable
This commit is contained in:
parent
0308732328
commit
8eba099146
3 changed files with 17 additions and 16 deletions
|
@ -6,6 +6,7 @@ package sop.operation;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
import java.nio.charset.CharacterCodingException;
|
||||||
|
|
||||||
import sop.Profile;
|
import sop.Profile;
|
||||||
import sop.Ready;
|
import sop.Ready;
|
||||||
|
@ -54,7 +55,11 @@ public interface GenerateKey {
|
||||||
default GenerateKey withKeyPassword(byte[] password)
|
default GenerateKey withKeyPassword(byte[] password)
|
||||||
throws SOPGPException.PasswordNotHumanReadable,
|
throws SOPGPException.PasswordNotHumanReadable,
|
||||||
SOPGPException.UnsupportedOption {
|
SOPGPException.UnsupportedOption {
|
||||||
return withKeyPassword(UTF8Util.decodeUTF8(password));
|
try {
|
||||||
|
return withKeyPassword(UTF8Util.decodeUTF8(password));
|
||||||
|
} catch (CharacterCodingException e) {
|
||||||
|
throw new SOPGPException.PasswordNotHumanReadable();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -4,8 +4,6 @@
|
||||||
|
|
||||||
package sop.util;
|
package sop.util;
|
||||||
|
|
||||||
import sop.exception.SOPGPException;
|
|
||||||
|
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.nio.CharBuffer;
|
import java.nio.CharBuffer;
|
||||||
import java.nio.charset.CharacterCodingException;
|
import java.nio.charset.CharacterCodingException;
|
||||||
|
@ -15,7 +13,8 @@ import java.nio.charset.CodingErrorAction;
|
||||||
|
|
||||||
public class UTF8Util {
|
public class UTF8Util {
|
||||||
|
|
||||||
private static final CharsetDecoder UTF8Decoder = Charset.forName("UTF8")
|
public static final Charset UTF8 = Charset.forName("UTF8");
|
||||||
|
private static final CharsetDecoder UTF8Decoder = UTF8
|
||||||
.newDecoder()
|
.newDecoder()
|
||||||
.onUnmappableCharacter(CodingErrorAction.REPORT)
|
.onUnmappableCharacter(CodingErrorAction.REPORT)
|
||||||
.onMalformedInput(CodingErrorAction.REPORT);
|
.onMalformedInput(CodingErrorAction.REPORT);
|
||||||
|
@ -28,13 +27,10 @@ public class UTF8Util {
|
||||||
*
|
*
|
||||||
* @return decoded string
|
* @return decoded string
|
||||||
*/
|
*/
|
||||||
public static String decodeUTF8(byte[] data) {
|
public static String decodeUTF8(byte[] data)
|
||||||
|
throws CharacterCodingException {
|
||||||
ByteBuffer byteBuffer = ByteBuffer.wrap(data);
|
ByteBuffer byteBuffer = ByteBuffer.wrap(data);
|
||||||
try {
|
CharBuffer charBuffer = UTF8Decoder.decode(byteBuffer);
|
||||||
CharBuffer charBuffer = UTF8Decoder.decode(byteBuffer);
|
return charBuffer.toString();
|
||||||
return charBuffer.toString();
|
|
||||||
} catch (CharacterCodingException e) {
|
|
||||||
throw new SOPGPException.PasswordNotHumanReadable();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,8 +5,8 @@
|
||||||
package sop.util;
|
package sop.util;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import sop.exception.SOPGPException;
|
|
||||||
|
|
||||||
|
import java.nio.charset.CharacterCodingException;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
@ -15,7 +15,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
public class UTF8UtilTest {
|
public class UTF8UtilTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testValidUtf8Decoding() {
|
public void testValidUtf8Decoding() throws CharacterCodingException {
|
||||||
String utf8String = "Hello, World\n";
|
String utf8String = "Hello, World\n";
|
||||||
String decoded = UTF8Util.decodeUTF8(utf8String.getBytes(StandardCharsets.UTF_8));
|
String decoded = UTF8Util.decodeUTF8(utf8String.getBytes(StandardCharsets.UTF_8));
|
||||||
|
|
||||||
|
@ -29,11 +29,11 @@ public class UTF8UtilTest {
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void testInvalidUtf8StringThrows() {
|
public void testInvalidUtf8StringThrows() {
|
||||||
assertThrows(SOPGPException.PasswordNotHumanReadable.class,
|
assertThrows(CharacterCodingException.class,
|
||||||
() -> UTF8Util.decodeUTF8(new byte[] {(byte) 0xa0, (byte) 0xa1}));
|
() -> UTF8Util.decodeUTF8(new byte[] {(byte) 0xa0, (byte) 0xa1}));
|
||||||
assertThrows(SOPGPException.PasswordNotHumanReadable.class,
|
assertThrows(CharacterCodingException.class,
|
||||||
() -> UTF8Util.decodeUTF8(new byte[] {(byte) 0xc0, (byte) 0xaf}));
|
() -> UTF8Util.decodeUTF8(new byte[] {(byte) 0xc0, (byte) 0xaf}));
|
||||||
assertThrows(SOPGPException.PasswordNotHumanReadable.class,
|
assertThrows(CharacterCodingException.class,
|
||||||
() -> UTF8Util.decodeUTF8(new byte[] {(byte) 0x80, (byte) 0xbf}));
|
() -> UTF8Util.decodeUTF8(new byte[] {(byte) 0x80, (byte) 0xbf}));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue