View Javadoc
1   package net.sumaris.server.service.crypto;
2   
3   /*-
4    * #%L
5    * SUMARiS:: Server
6    * %%
7    * Copyright (C) 2018 SUMARiS Consortium
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU General Public License as
11   * published by the Free Software Foundation, either version 3 of the
12   * License, or (at your option) any later version.
13   * 
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   * 
19   * You should have received a copy of the GNU General Public
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/gpl-3.0.html>.
22   * #L%
23   */
24  
25  import net.sumaris.core.service.crypto.CryptoService;
26  import net.sumaris.core.util.crypto.CryptoUtils;
27  import net.sumaris.core.util.crypto.KeyPair;
28  import net.sumaris.server.config.SumarisServerConfiguration;
29  import org.slf4j.Logger;
30  import org.slf4j.LoggerFactory;
31  import org.nuiton.i18n.I18n;
32  import org.springframework.beans.factory.annotation.Autowired;
33  import org.springframework.stereotype.Service;
34  import org.springframework.util.StringUtils;
35  
36  import java.nio.charset.Charset;
37  
38  @Service("serverCryptoService")
39  public class ServerCryptoServiceImpl extends net.sumaris.core.service.crypto.CryptoServiceImpl implements ServerCryptoService {
40  
41  
42      /* Logger */
43      private static final Logger log = LoggerFactory.getLogger(ServerCryptoServiceImpl.class);
44  
45      private static final Charset CHARSET_UTF8 = Charset.forName("UTF-8");
46  
47      private final SumarisServerConfiguration config;
48  
49      private final CryptoService cryptoService;
50  
51      private final KeyPair serverKeyPair;
52  
53      private final String serverPubkey;
54  
55      @Autowired
56      public ServerCryptoServiceImpl(SumarisServerConfiguration config, CryptoService cryptoService) {
57          this.cryptoService = cryptoService;
58          this.config = config;
59  
60          // Generate server keypair
61          if (StringUtils.isEmpty(config.getKeypairSalt()) || StringUtils.isEmpty(config.getKeypairSalt())) {
62              this.serverKeyPair = cryptoService.getRandomKeypair();
63              this.serverPubkey = CryptoUtils.encodeBase58(this.serverKeyPair.getPubKey());
64              log.warn(I18n.t("sumaris.server.keypair.pubkey.random", serverPubkey));
65          }
66          else {
67              this.serverKeyPair = cryptoService.getKeyPair(config.getKeypairSalt(), config.getKeypairPassword());
68              this.serverPubkey = CryptoUtils.encodeBase58(this.serverKeyPair.getPubKey());
69              log.warn(I18n.t("sumaris.server.keypair.pubkey", serverPubkey));
70          }
71      }
72  
73      @Override
74      public KeyPair getServerKeypair() {
75          return this.serverKeyPair;
76      }
77  
78      @Override
79      public String getServerPubkey() {
80          return this.serverPubkey;
81      }
82  
83      @Override
84      public String sign(String message) {
85          return this.sign(message, serverKeyPair.secretKey);
86      }
87  }