Skip to content
Merged
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
46 changes: 46 additions & 0 deletions core/src/main/java/app/notesr/core/util/CharUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,28 @@
import java.nio.charset.CharsetEncoder;
import java.util.Arrays;

/**
* Utility class for character and byte conversion operations with secure memory handling.
*
* <p>This utility provides methods to convert between character and byte arrays using
* specified charsets, with automatic memory cleanup to minimize sensitive data exposure.
* All buffers are securely cleared after use to prevent information leakage.
*/
public final class CharUtils {
/**
* Converts a character array to a byte array using the specified charset.
*
* <p>This method encodes the given character array to bytes using the provided charset.
* Both the character buffer and byte buffer are securely cleared after extraction to
* minimize sensitive data exposure in memory.
*
* @param chars the character array to convert. May be {@code null}.
* @param charset the charset to use for encoding
* @return a new byte array containing the encoded bytes, or {@code null} if the
* input character array is {@code null}
* @throws CharacterCodingException if the character sequence cannot be encoded
* using the specified charset
*/
public static byte[] charsToBytes(char[] chars, Charset charset)
throws CharacterCodingException {

Expand All @@ -39,6 +60,20 @@ public static byte[] charsToBytes(char[] chars, Charset charset)
return bytes;
}

/**
* Converts a byte array to a character array using the specified charset.
*
* <p>This method decodes the given byte array to characters using the provided charset.
* Both the byte buffer and character buffer are securely cleared after extraction to
* minimize sensitive data exposure in memory.
*
* @param bytes the byte array to convert. May be {@code null}.
* @param charset the charset to use for decoding
* @return a new character array containing the decoded characters, or {@code null} if the
* input byte array is {@code null}
* @throws CharacterCodingException if the byte sequence cannot be decoded
* using the specified charset
*/
public static char[] bytesToChars(byte[] bytes, Charset charset)
throws CharacterCodingException {

Expand All @@ -64,6 +99,17 @@ public static char[] bytesToChars(byte[] bytes, Charset charset)
return chars;
}

/**
* Checks whether the given character array contains any non-zero characters.
*
* <p>This method iterates through the character array and returns {@code true} if
* any character is not the null character ({@code '\0'}). This is useful for
* determining if sensitive data (like passwords) still exists in memory.
*
* @param chars the character array to check. May be {@code null}.
* @return {@code true} if the array contains at least one non-zero character,
* {@code false} if the array is {@code null}, empty, or contains only null characters
*/
public static boolean hasNonZeroChars(char[] chars) {
if (chars == null) {
return false;
Expand Down
Loading