Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/sourcetap/license/LicenseGenerator.java


1   /*
2    * $Id$
3    *
4    *  Copyright (c) 2003 SourceTap - www.sourcetap.com
5    *
6    *  Permission is hereby granted, free of charge, to any person obtaining a
7    *  copy of this software and associated documentation files (the "Software"),
8    *  to deal in the Software without restriction, including without limitation
9    *  the rights to use, copy, modify, merge, publish, distribute, sublicense,
10   *  and/or sell copies of the Software, and to permit persons to whom the
11   *  Software is furnished to do so, subject to the following conditions:
12   *
13   *  The above copyright notice and this permission notice shall be included
14   *  in all copies or substantial portions of the Software.
15   *
16   *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17   *  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18   *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19   *  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20   *  CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21   *  OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22   *  THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23   */
24  package com.sourcetap.license;
25  
26  import java.io.*;
27  import java.security.*;
28  import java.security.spec.*;
29  
30  
31  /**
32   * Generate an encrypted license
33   *
34   * @author Steve Fowler
35   * @version $Revision$
36   */
37  public class LicenseGenerator {
38  
39    public LicenseGenerator(){}
40  
41    /**
42     *  return an encrypted license by signing the license with the private key file and encoding the result
43     *  in Base64
44     * 
45     * @param license the unencrypted license key
46     * @param privateKeyFile the name of the file containing the private key
47     * @return a Base64 representation of the encrypted license
48     * @throws LicenseException
49     */
50    public static String generateLicense( String license, String privateKeyFile)
51      throws LicenseException 
52    {
53      try 
54      {
55        String signature = signMessage( license, privateKeyFile);
56        String licenseEncoded = Base64Coder.encode ( license )+ "#" +  signature;
57  
58        return licenseEncoded;
59      }
60      catch (Exception e)
61      {
62        throw new LicenseException( e.getMessage() );  
63      }
64    }
65  
66    private static String signMessage(String message, String privateKeyFile)
67      throws IOException, NoSuchAlgorithmException, NoSuchProviderException,
68      InvalidKeyException, SignatureException, InvalidKeySpecException 
69    {
70      if (message == null)
71        throw new SignatureException("No Message to sign");
72  
73      EncryptionUtil signer = new EncryptionUtil();
74      String signature = signer.sign(message, privateKeyFile);
75      return signature;
76    }
77  
78  
79    private static void saveSignature(String signature, String signatureFile)
80      throws IOException {
81          FileOutputStream sigfos = new FileOutputStream(signatureFile);
82          sigfos.write(signature.getBytes());
83          sigfos.close();
84    }
85  
86    /**
87     * read a private key from the specified file
88     * @param URI name of the file containing the private key
89     * @return private key read from file
90     * @throws LicenseException
91     */
92    public static PrivateKey readPrivateKey( String URI ) throws LicenseException
93    {
94      try
95      {
96        EncryptionUtil keyUtil = new EncryptionUtil();
97        PrivateKey privateKey = keyUtil.readPrivateKey( URI );
98        return privateKey;
99      }
100     catch (Exception e)
101     {
102       throw new LicenseException("Unable to read Key File:" + e.getMessage());
103     }
104   }
105 
106 }