2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.tapocontrol.internal.helpers;
15 import java.security.KeyPair;
16 import java.security.KeyPairGenerator;
17 import java.security.MessageDigest;
18 import java.security.NoSuchAlgorithmException;
19 import java.security.SecureRandom;
20 import java.security.interfaces.RSAPrivateKey;
21 import java.security.interfaces.RSAPublicKey;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
28 * Handler class for TAPO Credentials
30 * @author Christian Wild - Initial contribution
33 public class TapoCredentials {
35 private final Logger logger = LoggerFactory.getLogger(TapoCredentials.class);
36 private MimeEncode mimeEncoder;
37 private String encodedPassword = "";
38 private String encodedEmail = "";
39 private String publicKey = "";
40 private String privateKey = "";
41 private String username = "";
42 private String password = "";
48 public TapoCredentials() {
49 this.mimeEncoder = new MimeEncode();
55 * @param email E-Mail-adress of Tapo Cloud
56 * @param passowrd Password of Tapo Cloud
58 public TapoCredentials(String eMail, String password) {
59 this.mimeEncoder = new MimeEncode();
60 setCredectials(eMail, password);
66 * @param username username (eMail-adress) of Tapo Cloud
67 * @param passowrd Password of Tapo Cloud
69 public void setCredectials(String eMail, String password) {
71 this.username = eMail;
72 this.password = password;
73 encryptCredentials(eMail, password);
75 } catch (Exception e) {
76 logger.warn("error init credential class '{}'", e.toString());
81 * encrypt credentials.
83 * @param username username (eMail-adress) of Tapo Cloud
84 * @param passowrd Password of Tapo Cloud
86 private void encryptCredentials(String username, String password) throws Exception {
87 logger.trace("encrypt credentials for '{}'", username);
89 /* Password Encoding */
90 byte[] byteWord = password.getBytes();
91 this.encodedPassword = mimeEncoder.encodeToString(byteWord);
94 String encodedUser = this.shaDigestUsername(username);
95 byteWord = encodedUser.getBytes("UTF-8");
96 this.encodedEmail = mimeEncoder.encodeToString(byteWord);
103 public void createKeyPair() throws NoSuchAlgorithmException {
104 logger.trace("generating new keypair");
105 KeyPairGenerator instance = KeyPairGenerator.getInstance("RSA");
106 instance.initialize(1024, new SecureRandom());
107 KeyPair generateKeyPair = instance.generateKeyPair();
109 this.publicKey = new String(mimeEncoder.encode(((RSAPublicKey) generateKeyPair.getPublic()).getEncoded()));
110 this.privateKey = new String(mimeEncoder.encode(((RSAPrivateKey) generateKeyPair.getPrivate()).getEncoded()));
111 logger.trace("new privateKey: '{}'", this.privateKey);
112 logger.trace("new ublicKey: '{}'", this.publicKey);
119 private String shaDigestUsername(String str) throws NoSuchAlgorithmException {
120 byte[] bArr = str.getBytes();
121 byte[] digest = MessageDigest.getInstance("SHA1").digest(bArr);
123 StringBuilder sb = new StringBuilder();
124 for (byte b : digest) {
125 String hexString = Integer.toHexString(b & 255);
126 if (hexString.length() == 1) {
128 sb.append(hexString);
130 sb.append(hexString);
133 return sb.toString();
137 * RETURN ENCODED PASSWORD
140 public String getEncodedPassword() {
141 return encodedPassword;
145 * RETURN ENCODED E-MAIL
148 public String getEncodedEmail() {
156 public String getPassword() {
161 * RETURN Username (E-MAIL)
164 public String getUsername() {
171 * @return String -----BEGIN PRIVATE KEY-----\n%s\n-----END PRIVATE KEY-----
173 public String getPrivateKey() {
174 return String.format("-----BEGIN PRIVATE KEY-----%n%s%n-----END PRIVATE KEY-----%n", privateKey);
180 * @return String -----BEGIN PUBLIC KEY-----\n%s\n-----END PUBLIC KEY-----
182 public String getPublicKey() {
183 return String.format("-----BEGIN PUBLIC KEY-----%n%s%n-----END PUBLIC KEY-----%n", publicKey);
187 * RETURN PRIVATE-KEY (BYTES)
189 * @return UTF-8 coded byte[] with private key
191 public byte[] getPrivateKeyBytes() {
193 return privateKey.getBytes("UTF-8");
194 } catch (Exception e) {
200 * RETURN PUBLIC-KEY (BYTES)
202 * @return UTF-8 coded byte[] with private key
204 public byte[] getPublicKeyBytes() {
206 return publicKey.getBytes("UTF-8");
207 } catch (Exception e) {
213 * CHECK IF CREDENTIALS ARE SET
217 public Boolean areSet() {
218 return !(this.username.isEmpty() || this.password.isEmpty());