@@ -268,6 +268,36 @@ static boolean constantTimeEquals(byte[] obj, byte[] anotherArray) {
268268 }
269269 return result == 0 ;
270270 }
271+
272+ /**
273+ * Calculates the entropy factor of a byte array.
274+ * <p>
275+ * This implementation will not create a copy of the internal array and will only internally initialize
276+ * a int array with 256 elements as temporary buffer.
277+ *
278+ * @param array to calculate the entropy from
279+ * @return entropy factor, higher means higher entropy
280+ */
281+ static double entropy (byte [] array ) {
282+ final int [] buffer = new int [256 ];
283+ Arrays .fill (buffer , -1 );
284+
285+ for (byte element : array ) {
286+ int unsigned = 0xff & element ;
287+ if (buffer [unsigned ] == -1 ) {
288+ buffer [unsigned ] = 0 ;
289+ }
290+ buffer [unsigned ]++;
291+ }
292+
293+ double entropy = 0 ;
294+ for (int count : buffer ) {
295+ if (count == -1 ) continue ;
296+ double prob = (double ) count / array .length ;
297+ entropy -= prob * (Math .log (prob ) / Math .log (2 ));
298+ }
299+ return entropy ;
300+ }
271301 }
272302
273303 /**
@@ -683,58 +713,6 @@ static byte[] readFromFile(java.io.File file, int offset, int length) {
683713 private Util () {
684714 }
685715
686- /*
687- =================================================================================================
688- Copyright 2011 Twitter, Inc.
689- -------------------------------------------------------------------------------------------------
690- Licensed under the Apache License, Version 2.0 (the "License");
691- you may not use this work except in compliance with the License.
692- You may obtain a copy of the License in the LICENSE file, or at:
693-
694- http://www.apache.org/licenses/LICENSE-2.0
695-
696- Unless required by applicable law or agreed to in writing, software
697- distributed under the License is distributed on an "AS IS" BASIS,
698- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
699- See the License for the specific language governing permissions and
700- limitations under the License.
701- =================================================================================================
702- */
703-
704- /**
705- * Class that calculates the entropy factor
706- *
707- * @param <T>
708- */
709- @ SuppressWarnings ("WeakerAccess" )
710- static final class Entropy <T > {
711- private final Map <T , Integer > map = new HashMap <>();
712- private int total = 0 ;
713-
714- public Entropy (Iterable <T > elements ) {
715- for (T element : elements ) {
716- if (!map .containsKey (element )) {
717- map .put (element , 0 );
718- }
719- map .put (element , map .get (element ) + 1 );
720- total ++;
721- }
722- }
723-
724- private double Log2 (double n ) {
725- return Math .log (n ) / Math .log (2 );
726- }
727-
728- public double entropy () {
729- double entropy = 0 ;
730- for (int count : map .values ()) {
731- double prob = (double ) count / total ;
732- entropy -= prob * Log2 (prob );
733- }
734- return entropy ;
735- }
736- }
737-
738716 /**
739717 * A simple iterator for the bytes class, which does not support remove
740718 */
0 commit comments