// Path: src/main/java/com/adeptj/runtime/common/Constants.java
// public static final String H2_MAP_ADMIN_CREDENTIALS = "adminCredentials";
// 
// Path: src/main/java/com/adeptj/runtime/common/Constants.java
// public static final String MV_CREDENTIALS_STORE = "credentials.dat";


import java.util.Arrays;
import java.util.Base64;
import static com.adeptj.runtime.common.Constants.H2_MAP_ADMIN_CREDENTIALS;
import static com.adeptj.runtime.common.Constants.MV_CREDENTIALS_STORE;
import static java.nio.charset.StandardCharsets.UTF_8;
import io.undertow.security.idm.Credential;
import io.undertow.security.idm.PasswordCredential;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.h2.mvstore.MVStore;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/*
###############################################################################
#                                                                             # 
#    Copyright 2016, AdeptJ (http://www.adeptj.com)                           #
#                                                                             #
#    Licensed under the Apache License, Version 2.0 (the "License");          #
#    you may not use this file except in compliance with the License.         #
#    You may obtain a copy of the License at                                  #
#                                                                             #
#        http://www.apache.org/licenses/LICENSE-2.0                           #
#                                                                             #
#    Unless required by applicable law or agreed to in writing, software      #
#    distributed under the License is distributed on an "AS IS" BASIS,        #
#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
#    See the License for the specific language governing permissions and      #
#    limitations under the License.                                           #
#                                                                             #
###############################################################################
*/

package com.adeptj.runtime.server;




/**
 * CredentialMatcher - utility creates an SHA-256 digest of the supplied password and matches with the one stored
 * in MVStore.
 *
 * @author Rakesh.Kumar, AdeptJ
 */
final class CredentialMatcher {

    private static final String SHA_256 = "SHA-256";

    private CredentialMatcher() {
    }

    static boolean match(String username, Credential credential) {
        char[] inputPwd = ((PasswordCredential) credential).getPassword();
        if (StringUtils.isEmpty(username) || ArrayUtils.isEmpty(inputPwd)) {
            return false;
        }
        byte[] inputPwdBytes = null;
        byte[] digest = null;
        byte[] storedPwdBytes = null;
// Path: src/main/java/com/adeptj/runtime/common/Constants.java
// public static final String H2_MAP_ADMIN_CREDENTIALS = "adminCredentials";
// 
// Path: src/main/java/com/adeptj/runtime/common/Constants.java
// public static final String MV_CREDENTIALS_STORE = "credentials.dat";


// Path: src/main/java/com/adeptj/runtime/server/CredentialMatcher.java
import java.util.Arrays;
import java.util.Base64;
import static com.adeptj.runtime.common.Constants.H2_MAP_ADMIN_CREDENTIALS;
import static com.adeptj.runtime.common.Constants.MV_CREDENTIALS_STORE;
import static java.nio.charset.StandardCharsets.UTF_8;
import io.undertow.security.idm.Credential;
import io.undertow.security.idm.PasswordCredential;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.h2.mvstore.MVStore;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/*
###############################################################################
#                                                                             # 
#    Copyright 2016, AdeptJ (http://www.adeptj.com)                           #
#                                                                             #
#    Licensed under the Apache License, Version 2.0 (the "License");          #
#    you may not use this file except in compliance with the License.         #
#    You may obtain a copy of the License at                                  #
#                                                                             #
#        http://www.apache.org/licenses/LICENSE-2.0                           #
#                                                                             #
#    Unless required by applicable law or agreed to in writing, software      #
#    distributed under the License is distributed on an "AS IS" BASIS,        #
#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
#    See the License for the specific language governing permissions and      #
#    limitations under the License.                                           #
#                                                                             #
###############################################################################
*/

package com.adeptj.runtime.server;




/**
 * CredentialMatcher - utility creates an SHA-256 digest of the supplied password and matches with the one stored
 * in MVStore.
 *
 * @author Rakesh.Kumar, AdeptJ
 */
final class CredentialMatcher {

    private static final String SHA_256 = "SHA-256";

    private CredentialMatcher() {
    }

    static boolean match(String username, Credential credential) {
        char[] inputPwd = ((PasswordCredential) credential).getPassword();
        if (StringUtils.isEmpty(username) || ArrayUtils.isEmpty(inputPwd)) {
            return false;
        }
        byte[] inputPwdBytes = null;
        byte[] digest = null;
        byte[] storedPwdBytes = null;
        try (MVStore store = MVStore.open(MV_CREDENTIALS_STORE)) {