]> git.basschouten.com Git - openhab-addons.git/blob
b7a2cb33b70b34a4bbb01948410e1bbe1ea9b41a
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.pjlinkdevice.internal.device.command.authentication;
14
15 import java.math.BigInteger;
16 import java.security.MessageDigest;
17 import java.security.NoSuchAlgorithmException;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.binding.pjlinkdevice.internal.device.command.AuthenticationException;
21 import org.openhab.binding.pjlinkdevice.internal.device.command.Request;
22 import org.openhab.binding.pjlinkdevice.internal.device.command.Response;
23
24 /**
25  * Calculates the response matching the callenge received from the PJLink device.
26  *
27  * @author Nils Schnabel - Initial contribution
28  */
29 @NonNullByDefault
30 public class AuthenticationRequest<ResponseType extends Response<?>> implements Request {
31
32     private AuthenticationCommand<ResponseType> command;
33
34     public AuthenticationRequest(AuthenticationCommand<ResponseType> command) {
35         this.command = command;
36     }
37
38     @Override
39     public String getRequestString() throws AuthenticationException {
40         try {
41             MessageDigest md = MessageDigest.getInstance("MD5");
42             String toBeDigested = (this.command.getChallenge() + this.command.getDevice().getAdminPassword());
43             byte[] digest = md.digest(toBeDigested.getBytes());
44             BigInteger bigInt = new BigInteger(1, digest);
45             return bigInt.toString(16);
46         } catch (NoSuchAlgorithmException e) {
47             throw new AuthenticationException(e);
48         }
49     }
50 }