// Path: src/main/java/com/github/pgasync/message/Message.java
// public interface Message {
// 
// }


import com.github.pgasync.message.Message;
import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import static javax.xml.bind.DatatypeConverter.printHexBinary;
/*
 * 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.github.pgasync.message.frontend;




/**
 * @author Antti Laisi
 */
public class PasswordMessage implements Message {

    private final String password;
    private final byte[] passwordHash;

    public PasswordMessage(String username, String password, byte[] md5salt, Charset encoding) {
        this.password = password;
        this.passwordHash = md5salt != null ? md5(username, password, md5salt, encoding) : null;
    }

    public byte[] getPasswordHash() {
        return passwordHash;
    }

    public String getPassword() {
        return password;
    }

    private static byte[] md5(String username, String password, byte[] md5salt, Charset encoding) {
        MessageDigest md5 = md5();
        md5.update(password.getBytes(encoding));
// Path: src/main/java/com/github/pgasync/message/Message.java
// public interface Message {
// 
// }


// Path: src/main/java/com/github/pgasync/message/frontend/PasswordMessage.java
import com.github.pgasync.message.Message;
import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import static javax.xml.bind.DatatypeConverter.printHexBinary;

/*
 * 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.github.pgasync.message.frontend;




/**
 * @author Antti Laisi
 */
public class PasswordMessage implements Message {

    private final String password;
    private final byte[] passwordHash;

    public PasswordMessage(String username, String password, byte[] md5salt, Charset encoding) {
        this.password = password;
        this.passwordHash = md5salt != null ? md5(username, password, md5salt, encoding) : null;
    }

    public byte[] getPasswordHash() {
        return passwordHash;
    }

    public String getPassword() {
        return password;
    }

    private static byte[] md5(String username, String password, byte[] md5salt, Charset encoding) {
        MessageDigest md5 = md5();
        md5.update(password.getBytes(encoding));
        md5.update(username.getBytes(encoding));