package com.bendoerr.saltedmocha;

import static com.bendoerr.saltedmocha.CryptoException.exceptionOf;

public class Util {

    public static final int MAX_ARRAY_SIZE;

    static {
        int max = Integer.MAX_VALUE - 2;
        try {
            byte[] bytes = new byte[max];
        } catch (java.lang.OutOfMemoryError oom) {
            // Probably OK. Need to test on 32bit JVM.
            // Reports say it's somewhere around 1.1 billion.
            max = Integer.MAX_VALUE / 2;
        }
        MAX_ARRAY_SIZE = max;
    }

    public static void checkedArrayCopy(Object src, int srcPos, Object dst, int dstPos, int len) throws CryptoException {
        try {
            System.arraycopy(src, srcPos, dst, dstPos, len);
        } catch (RuntimeException re) {
            throw exceptionOf(re);
        }
    }

    public static void validateLength(byte[] a, int expected, String what, String expectedWhat) throws CryptoException {
        if (a.length != expected)
            throw exceptionOf(new IllegalArgumentException(
                    what + " length(" + a.length + ") is not " + expectedWhat + "(" + expected + ")"));
    }

}

@@ -1144,10 +1144,435 @@
 %0A    %7D%0A%0A
+    /**%0A     * Approximation of libsodium's %3Ccode%3Esodium_memzero%3C/code%3E. For us in%0A     * Javaland it looks like %3Ccode%3Efill(array, (byte) 0)%3C/code%3E%0A     *%0A     * @param array to zero out.%0A     *%0A     * @see %3Ca href=%22https://download.libsodium.org/doc/helpers/memory_management.html#zeroing-memory%22%3ELibsodium Zeroing Memory%3C/a%3E%0A     */%0A    public static void java_memzero(byte%5B%5D array) %7B%0A        fill(array, (byte) 0);%0A    %7D%0A%0A
 %7D%0A

Add approximation of libsodium's sodium_memzero.