Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

import com.ironz.binaryprefs.exception.EncryptionException;

import java.nio.charset.Charset;
import java.util.Arrays;

public final class XorKeyEncryption implements KeyEncryption {

private static final Charset CHARSET = Charset.forName("UTF-8");

private static final String SMALL_XOR_MESSAGE = "XOR must be at least 16 bytes";
private static final String MIRRORED_XOR_MESSAGE = "XOR must not be mirrored";

Expand Down Expand Up @@ -47,7 +50,7 @@ private boolean isEven() {

@Override
public String encrypt(String name) {
byte[] original = name.getBytes();
byte[] original = name.getBytes(CHARSET);
byte[] bytes = xorName(original);
return safeEncoder.encodeToString(bytes);
}
Expand All @@ -56,7 +59,7 @@ public String encrypt(String name) {
public String decrypt(String name) {
byte[] decode = safeEncoder.decode(name);
byte[] bytes = xorName(decode);
return new String(bytes);
return new String(bytes, CHARSET);
}

private byte[] xorName(byte[] original) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package com.ironz.binaryprefs.serialization.serializer;

import java.nio.charset.Charset;

/**
* {@code String} to byte array implementation and backwards
*/
public final class StringSerializer {

private static final Charset CHARSET = Charset.forName("UTF-8");

/**
* Uses for detecting byte array type of {@link String}
*/
Expand All @@ -27,7 +31,7 @@ public final class StringSerializer {
* @return specific byte array with scheme.
*/
public byte[] serialize(String s) {
byte[] stringBytes = s.getBytes();
byte[] stringBytes = s.getBytes(CHARSET);
byte[] b = new byte[stringBytes.length + FLAG_OFFSET];
b[0] = FLAG;
System.arraycopy(stringBytes, 0, b, FLAG_OFFSET, stringBytes.length);
Expand All @@ -53,7 +57,7 @@ public String deserialize(byte[] bytes) {
* @return deserialized String
*/
public String deserialize(byte[] bytes, int offset, int length) {
return new String(bytes, FLAG_OFFSET + offset, length);
return new String(bytes, FLAG_OFFSET + offset, length, CHARSET);
}

public boolean isMatches(byte flag) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.ironz.binaryprefs.serialization.serializer;

import java.nio.charset.Charset;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
Expand All @@ -9,6 +10,8 @@
*/
public final class StringSetSerializer {

private static final Charset CHARSET = Charset.forName("UTF-8");

/**
* Uses for detecting byte array type of {@link Set} of {@link String}
*/
Expand All @@ -32,7 +35,7 @@ public byte[] serialize(Set<String> set) {
int totalArraySize = 1;

for (String s : set) {
byte[] stringBytes = s.getBytes();
byte[] stringBytes = s.getBytes(CHARSET);
byte[] stringSizeBytes = intToBytes(stringBytes.length);

byte[] merged = new byte[stringBytes.length + stringSizeBytes.length];
Expand Down Expand Up @@ -96,7 +99,7 @@ public Set<String> deserialize(byte[] bytes) {
stringBytes[k] = bytes[stringOffset];
}

set.add(new String(stringBytes));
set.add(new String(stringBytes, CHARSET));

i += integerBytesSize + stringSize;
}
Expand Down