@@ -0,0 +1,2879 @@
+/*%0A * Copyright 2016 Lime - HighTech Solutions s.r.o.%0A *%0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A *%0A * http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A */%0Apackage io.getlime.security.powerauth.crypto.server.util;%0A%0Aimport com.google.common.collect.Lists;%0Aimport io.getlime.security.powerauth.crypto.lib.generator.KeyGenerator;%0Aimport io.getlime.security.powerauth.crypto.lib.util.HMACHashUtilities;%0A%0Aimport java.io.UnsupportedEncodingException;%0Aimport java.math.BigInteger;%0Aimport java.util.List;%0A%0A/**%0A * This is a helper class that provides utility methods for computing various data digests.%0A *%0A * @author Petr Dvorak, petr@lime-company.eu%0A */%0Apublic class DataDigest %7B%0A%0A    /**%0A     * Data digest result.%0A     */%0A    public class Result %7B%0A        private String digest;%0A        private byte%5B%5D salt;%0A%0A        Result(String digest, byte%5B%5D salt) %7B%0A            this.digest = digest;%0A            this.salt = salt;%0A        %7D%0A%0A        public String getDigest() %7B%0A            return digest;%0A        %7D%0A%0A        public byte%5B%5D getSalt() %7B%0A            return salt;%0A        %7D%0A%0A    %7D%0A%0A    private static final int AUTHORIZATION_CODE_LENGTH = 8;%0A%0A    private HMACHashUtilities hmac = new HMACHashUtilities();%0A%0A    /**%0A     * Data digest of the list with string elements. Data is first normalized (items concatenated%0A     * by '&' character), then a random key is generated and hash (HMAC-SHA256) is computed. Finally,%0A     * the resulting MAC is decimalized to the signature of a length 8 numeric digits.%0A     *%0A     * @param items Items to be serialized into digest.%0A     * @return Digest fo provided data, including seed used to compute that digest.%0A     */%0A    public Result generateDigest(List%3CString%3E items) %7B%0A        try %7B%0A            if (items.size() == 0) %7B%0A                return null;%0A            %7D%0A            byte%5B%5D operationData = String.join(%22&%22, items).getBytes(%22UTF-8%22);%0A            byte%5B%5D randomKey = new KeyGenerator().generateRandomBytes(16);%0A            byte%5B%5D otpHash = hmac.hash(randomKey, operationData);%0A            BigInteger otp = new BigInteger(otpHash).mod(BigInteger.TEN.pow(AUTHORIZATION_CODE_LENGTH));%0A            String digitFormat = %22%25%22 + String.format(%22%2502d%22, AUTHORIZATION_CODE_LENGTH) + %22d%22;%0A            String digest = String.format(digitFormat, otp);%0A            return new Result(digest, randomKey);%0A        %7D catch (UnsupportedEncodingException ex) %7B%0A            return null;%0A        %7D%0A    %7D%0A%0A%7D%0A

Fix #140: Create a helper method for list data digest