1 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 32 class PasswordRecord extends WritableRecordData 33 { 34 37 private String password; 38 41 private byte[] data; 42 43 48 public PasswordRecord(String 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 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 94 public byte[] getData() 95 { 96 return data; 97 } 98 99 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 |