KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > HelloLicenseKeySupport


1 import java.io.*;
2 import com.memoire.vainstall.*;
3 import Acme.Crypto.*;
4 /*
5  * I suggest to license this file under LGPL license so anyone
6  * could extend this class with own license key validators w/o need
7  * to release source code of validator. I suggest to no to license
8  * this file under GPL2 and stick with LGPL as otherwise it will put
9  * very much of burden on the users of vainstall w/o obvious value
10  * to other user as different company polices could require different
11  * fields to be supplied and this will be likely only difference in different
12  * validators.
13  *
14  * copyrights are completly transfered to VAInstall team without any
15  * restriction.
16  */

17 /**
18   * this class is default implementation of license key support, that does nothing.
19   */

20 public class HelloLicenseKeySupport extends LicenseKeySupport
21 {
22   String JavaDoc key = "HW0123-4567-89AB-CDEF-8B58-85B7";
23   String JavaDoc codekey;
24   /** @return true if license key query step need to be performed
25     */

26   public boolean needsLicenseKey() {
27     return true;
28   }
29   /** @return uri of page that contains registration page,
30     * if such uri is not null, then it will be shown
31     * to user and launch browser button will be displayed
32     * depending on ui and platform.
33     */

34
35   public String JavaDoc getRegistrationPage() {
36     return "http://www.novosoft-us.com";
37   }
38
39   /** get field info for this installer
40     * @return array of field info.
41     */

42   public FieldInfo[] getFieldInfo() {
43     return new FieldInfo[]{new FieldInfo("serial key", 20, key)};
44   }
45   /** set field values, this method coudl be called any number of times.
46     * implementation of this class should keep last passed values.
47     * @param values array of strings where each element correspond field
48     * info returned from get field info.
49     */

50   public void setFieldValues(String JavaDoc values[]) {
51     key = values[0];
52   }
53   /** @return true, if license key is valid, this method should be called only
54     * after set field values were called.
55     */

56   public boolean isLicenseKeyValid()
57   {
58     StringBuffer JavaDoc tmp = new StringBuffer JavaDoc(key.toUpperCase());
59     for(int n = tmp.length(),i=0;i<n;i++) {
60       if(tmp.charAt(i) == '-'){
61         tmp.deleteCharAt(i);
62         i--;
63         n--;
64       }
65     }
66     String JavaDoc normalized = tmp.toString();
67     if(normalized.length() < 26) {
68       return false;
69     }
70     codekey = normalized.substring(0,normalized.length()-8);
71     if(codekey.hashCode() != (int)Long.parseLong(normalized.substring(codekey.length(),normalized.length()),16) ){
72       return false;
73     }
74     return true;
75   }
76   /** encode archive.zip stream with key supplied as string in
77     * configuration file, this fucntion is called during building install
78     * package
79     * @param is input steam to encode
80     * @param key key supplied in configuration file
81     * @return encrypted stream
82     */

83   public OutputStream encodeStream(OutputStream os, String JavaDoc key) throws IOException
84   {
85     EncryptedOutputStream rc = new EncryptedOutputStream(new AesCipher(key), os);
86     return rc;
87   }
88   /** decode archive.zip stream using infromation supplied in fieldValues
89     * @param is input steam to decode
90     * @return decrypted stream
91     */

92   public InputStream decodeStream(InputStream is) throws IOException
93   {
94     EncryptedInputStream rc = new EncryptedInputStream(new AesCipher(codekey), is);
95     return rc;
96   }
97
98 }
Popular Tags