/****************************************************************************
 * Copyright (C) 2012 ecsec GmbH.
 * All rights reserved.
 * Contact: ecsec GmbH (info@ecsec.de)
 *
 * This file is part of the Open eCard App.
 *
 * GNU General Public License Usage
 * This file may be used under the terms of the GNU General Public
 * License version 3.0 as published by the Free Software Foundation
 * and appearing in the file LICENSE.GPL included in the packaging of
 * this file. Please review the following information to ensure the
 * GNU General Public License version 3.0 requirements will be met:
 * http://www.gnu.org/copyleft/gpl.html.
 *
 * Other Usage
 * Alternatively, this file may be used in accordance with the terms
 * and conditions contained in a signed written agreement between
 * you and ecsec GmbH.
 *
 ***************************************************************************/

package org.openecard.common.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 *
 * @author Tobias Wich <tobias.wich@ecsec.de>
 */
public class ValueValidator {

    /**
     * Check if the value contains at least the given number of bytes as hex digits.
     *
     * @param value Value to test for hex digits.
     * @param numBytes Number of bytes that must be present in the value.
     * @return true if enough bytes are present, false otherwise.
     */
    public static boolean checkHexStrength(String value, int numBytes) {
	Pattern p = Pattern.compile("\\p{XDigit}{2}");
	Matcher m = p.matcher(value);

	int count = 0;
	while (m.find()) {
	    count++;
	}

	return count >= numBytes;
    }

    /**
     * Check if the psk value is strong enough.
     * PSKs must at least contain 16 bytes as hex digits.
     *
     * @param psk PSK value to test.
     * @return true if psk is strong enough, false otherwise.
     */
    public static boolean checkPSKStrength(String psk) {
	return checkHexStrength(psk, 16);
    }
    /**
     * Check if the session ID value is strong enough.
     * Session IDs must at least contain 16 bytes as hex digits. Usually UUIDs are used.
     *
     * @param session Session ID value to test.
     * @return true if session ID is strong enough, false otherwise.
     */
    public static boolean checkSessionStrength(String session) {
	return checkHexStrength(session, 16);
    }

}

@@ -1176,32 +1176,114 @@
  digits.%0A     *%0A
+     * @deprecated Renamed to %7B@link #checkHexByteStrength(java.lang.String, int)%0A
      * @param va
@@ -1312,24 +1312,24 @@
 hex digits.%0A
-
      * @para
@@ -1456,32 +1456,48 @@
 erwise.%0A     */%0A
+    @Deprecated%0A
     public stati
@@ -1553,16 +1553,902 @@
 ytes) %7B%0A
+%09return checkHexNibbleStrength(value, numBytes * 2);%0A    %7D%0A    /**%0A     * Check if the value contains at least the given number of bytes as hex digits.%0A     *%0A     * @param value Value to test for hex digits.%0A     * @param numBytes Number of bytes that must be present in the value.%0A     * @return true if enough bytes are present, false otherwise.%0A     */%0A    public static boolean checkHexByteStrength(String value, int numBytes) %7B%0A%09return checkHexNibbleStrength(value, numBytes * 2);%0A    %7D%0A%0A    /**%0A     * Check if the value contains at least the given number of half-bytes as hex digits.%0A     *%0A     * @param value Value to test for hex digits.%0A     * @param numNibbles  Number of half-bytes that must be present in the value.%0A     * @return true if enough bytes are present, false otherwise.%0A     */%0A    public static boolean checkHexNibbleStrength(String value, int numNibbles) %7B%0A
 %09Pattern
@@ -2481,17 +2481,17 @@
 XDigit%7D%7B
-2
+1
 %7D%22);%0A%09Ma
@@ -2593,19 +2593,21 @@
 t %3E= num
-Byt
+Nibbl
 es;%0A    
@@ -2902,32 +2902,36 @@
 %09return checkHex
+Byte
 Strength(psk, 16
@@ -3035,35 +3035,40 @@
 t least contain 
-16 
+25 half-
 bytes as hex dig
@@ -3076,30 +3076,130 @@
 ts. 
-Usually UUIDs are used
+BSI TR-03112 sec. 3.7.1 demands that session IDs%0A     * must have at least 100 bits of entropy which is exactly 25 nibbles
 .%0A  
@@ -3401,32 +3401,38 @@
 %09return checkHex
+Nibble
 Strength(session
@@ -3433,18 +3433,18 @@
 ession, 
-16
+25
 );%0A    %7D

Set session ID strength to be at least 100 bit (25 half-bytes)