KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jxl > write > biff > PasswordRecord


1 /*********************************************************************
2 *
3 * Copyright (C) 2002 Andrew Khan
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 ***************************************************************************/

19
20 package jxl.write.biff;
21
22 import common.Assert;
23
24 import jxl.biff.Type;
25 import jxl.biff.IntegerHelper;
26 import jxl.biff.WritableRecordData;
27
28 /**
29  * A password record. Thanks to Michael Matthews for sending me the
30  * code to actually store the password for the sheet
31  */

32 class PasswordRecord extends WritableRecordData
33 {
34   /**
35    * The password
36    */

37   private String JavaDoc password;
38   /**
39    * The binary data
40    */

41   private byte[] data;
42
43   /**
44    * Constructor
45    *
46    * @param pw the password
47    */

48   public PasswordRecord(String JavaDoc pw)
49   {
50     super(Type.PASSWORD);
51
52     password = pw;
53
54     if (pw == null)
55     {
56       data = new byte[2];
57       IntegerHelper.getTwoBytes(0, data, 0);
58     }
59     else
60     {
61       byte [] passwordBytes = pw.getBytes();
62       int passwordHash = 0;
63       for (int a = 0; a < passwordBytes.length; a++)
64       {
65        int shifted = rotLeft15Bit(passwordBytes[a], a + 1);
66        passwordHash ^= shifted;
67       }
68       passwordHash ^= passwordBytes.length;
69       passwordHash ^= 0xCE4B;
70
71       data = new byte[2];
72       IntegerHelper.getTwoBytes(passwordHash, data, 0);
73     }
74   }
75
76   /**
77    * Constructor
78    *
79    * @param ph the password hash code
80    */

81   public PasswordRecord(int ph)
82   {
83     super(Type.PASSWORD);
84
85     data = new byte[2];
86     IntegerHelper.getTwoBytes(ph, data, 0);
87   }
88
89   /**
90    * Gets the binary data for output to file
91    *
92    * @return the binary data
93    */

94   public byte[] getData()
95   {
96     return data;
97   }
98
99   /**
100    * Rotate the value by 15 bits. Thanks to Michael for this
101    *
102    * @param val
103    * @param rotate
104    * @return int
105    */

106   private int rotLeft15Bit(int val, int rotate)
107   {
108     val = val &0x7FFF;
109
110     for(; rotate > 0; rotate--)
111     {
112       if((val & 0x4000) != 0)
113       {
114         val = ((val << 1) & 0x7FFF) + 1;
115       }
116       else
117       {
118         val = (val << 1) & 0x7FFF;
119       }
120     }
121
122     return val;
123   }
124 }
125
Popular Tags