You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PasswordRecord.java 3.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.hssf.record;
  16. import org.apache.poi.poifs.crypt.CryptoFunctions;
  17. import org.apache.poi.util.HexDump;
  18. import org.apache.poi.util.LittleEndianOutput;
  19. /**
  20. * Title: Password Record (0x0013)<p/>
  21. * Description: stores the encrypted password for a sheet or workbook (HSSF doesn't support encryption)
  22. * REFERENCE: PG 371 Microsoft Excel 97 Developer's Kit (ISBN: 1-57231-498-2)<p/>
  23. * @author Andrew C. Oliver (acoliver at apache dot org)
  24. *
  25. */
  26. public final class PasswordRecord extends StandardRecord {
  27. public final static short sid = 0x0013;
  28. private int field_1_password; // not sure why this is only 2 bytes, but it is... go figure
  29. public PasswordRecord(int password) {
  30. field_1_password = password;
  31. }
  32. public PasswordRecord(RecordInputStream in) {
  33. field_1_password = in.readShort();
  34. }
  35. /**
  36. * Return the password hash
  37. *
  38. * @deprecated use {@link CryptoFunctions#createXorVerifier1(String)}
  39. */
  40. public static short hashPassword(String password) {
  41. return (short)CryptoFunctions.createXorVerifier1(password);
  42. }
  43. /**
  44. * set the password
  45. *
  46. * @param password representing the password
  47. */
  48. public void setPassword(int password) {
  49. field_1_password = password;
  50. }
  51. /**
  52. * get the password
  53. *
  54. * @return short representing the password
  55. */
  56. public int getPassword() {
  57. return field_1_password;
  58. }
  59. public String toString() {
  60. StringBuffer buffer = new StringBuffer();
  61. buffer.append("[PASSWORD]\n");
  62. buffer.append(" .password = ").append(HexDump.shortToHex(field_1_password)).append("\n");
  63. buffer.append("[/PASSWORD]\n");
  64. return buffer.toString();
  65. }
  66. public void serialize(LittleEndianOutput out) {
  67. out.writeShort(field_1_password);
  68. }
  69. protected int getDataSize() {
  70. return 2;
  71. }
  72. public short getSid() {
  73. return sid;
  74. }
  75. /**
  76. * Clone this record.
  77. */
  78. public Object clone() {
  79. return new PasswordRecord(field_1_password);
  80. }
  81. }