diff --git a/core/src/main/java/app/notesr/core/util/CharUtils.java b/core/src/main/java/app/notesr/core/util/CharUtils.java index 660c1bc8..3eea192d 100644 --- a/core/src/main/java/app/notesr/core/util/CharUtils.java +++ b/core/src/main/java/app/notesr/core/util/CharUtils.java @@ -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. + * + *

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. + * + *

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 { @@ -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. + * + *

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 { @@ -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. + * + *

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;