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.

EncryptionVerifier.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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.poifs.crypt;
  16. import java.util.Collections;
  17. import java.util.LinkedHashMap;
  18. import java.util.Map;
  19. import java.util.function.Supplier;
  20. import org.apache.poi.common.Duplicatable;
  21. import org.apache.poi.common.usermodel.GenericRecord;
  22. /**
  23. * Used when checking if a key is valid for a document
  24. */
  25. public abstract class EncryptionVerifier implements GenericRecord, Duplicatable {
  26. private byte[] salt;
  27. private byte[] encryptedVerifier;
  28. private byte[] encryptedVerifierHash;
  29. private byte[] encryptedKey;
  30. // protected int verifierHashSize;
  31. private int spinCount;
  32. private CipherAlgorithm cipherAlgorithm;
  33. private ChainingMode chainingMode;
  34. private HashAlgorithm hashAlgorithm;
  35. protected EncryptionVerifier() {}
  36. protected EncryptionVerifier(EncryptionVerifier other) {
  37. salt = (other.salt == null) ? null : other.salt.clone();
  38. encryptedVerifier = (other.encryptedVerifier == null) ? null : other.encryptedVerifier.clone();
  39. encryptedVerifierHash = (other.encryptedVerifierHash == null) ? null : other.encryptedVerifierHash.clone();
  40. encryptedKey = (other.encryptedKey == null) ? null : other.encryptedKey.clone();
  41. spinCount = other.spinCount;
  42. cipherAlgorithm = other.cipherAlgorithm;
  43. chainingMode = other.chainingMode;
  44. hashAlgorithm = other.hashAlgorithm;
  45. }
  46. public byte[] getSalt() {
  47. return salt;
  48. }
  49. public byte[] getEncryptedVerifier() {
  50. return encryptedVerifier;
  51. }
  52. public byte[] getEncryptedVerifierHash() {
  53. return encryptedVerifierHash;
  54. }
  55. public int getSpinCount() {
  56. return spinCount;
  57. }
  58. public byte[] getEncryptedKey() {
  59. return encryptedKey;
  60. }
  61. public CipherAlgorithm getCipherAlgorithm() {
  62. return cipherAlgorithm;
  63. }
  64. public HashAlgorithm getHashAlgorithm() {
  65. return hashAlgorithm;
  66. }
  67. public ChainingMode getChainingMode() {
  68. return chainingMode;
  69. }
  70. public void setSalt(byte[] salt) {
  71. this.salt = (salt == null) ? null : salt.clone();
  72. }
  73. public void setEncryptedVerifier(byte[] encryptedVerifier) {
  74. this.encryptedVerifier = (encryptedVerifier == null) ? null : encryptedVerifier.clone();
  75. }
  76. public void setEncryptedVerifierHash(byte[] encryptedVerifierHash) {
  77. this.encryptedVerifierHash = (encryptedVerifierHash == null) ? null : encryptedVerifierHash.clone();
  78. }
  79. public void setEncryptedKey(byte[] encryptedKey) {
  80. this.encryptedKey = (encryptedKey == null) ? null : encryptedKey.clone();
  81. }
  82. public void setSpinCount(int spinCount) {
  83. this.spinCount = spinCount;
  84. }
  85. public void setCipherAlgorithm(CipherAlgorithm cipherAlgorithm) {
  86. this.cipherAlgorithm = cipherAlgorithm;
  87. }
  88. public void setChainingMode(ChainingMode chainingMode) {
  89. this.chainingMode = chainingMode;
  90. }
  91. public void setHashAlgorithm(HashAlgorithm hashAlgorithm) {
  92. this.hashAlgorithm = hashAlgorithm;
  93. }
  94. public abstract EncryptionVerifier copy();
  95. @Override
  96. public Map<String, Supplier<?>> getGenericProperties() {
  97. final Map<String,Supplier<?>> m = new LinkedHashMap<>();
  98. m.put("salt", this::getSalt);
  99. m.put("encryptedVerifier", this::getEncryptedVerifier);
  100. m.put("encryptedVerifierHash", this::getEncryptedVerifierHash);
  101. m.put("encryptedKey", this::getEncryptedKey);
  102. m.put("spinCount", this::getSpinCount);
  103. m.put("cipherAlgorithm", this::getCipherAlgorithm);
  104. m.put("chainingMode", this::getChainingMode);
  105. m.put("hashAlgorithm", this::getHashAlgorithm);
  106. return Collections.unmodifiableMap(m);
  107. }
  108. }