- Timestamp:
- 07/24/19 11:23:45 (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/expeditee/encryption/io/EncryptedExpReader.java
r1414 r1415 24 24 import javax.crypto.spec.SecretKeySpec; 25 25 26 import org.expeditee.core.Point; 26 27 import org.expeditee.encryption.CryptographyConstants; 27 28 import org.expeditee.encryption.items.surrogates.EncryptionDetail; … … 33 34 import org.expeditee.io.DefaultFrameWriter; 34 35 import org.expeditee.io.ExpReader; 36 import org.expeditee.items.Constraint; 35 37 import org.expeditee.items.Item; 36 38 import org.expeditee.items.Text; … … 44 46 private SecretKey personalKey; 45 47 private boolean accessDenied = false; 48 private boolean _readingSurrogates; 49 private static final Predicate<String> endOfSection = s -> s.equals(EncryptedExpWriter.TERMINATOR + "") || s.equals(EncryptedExpWriter.TERMINATOR_WITH_CONTINUATION); 46 50 47 51 public static boolean isEncryptedExpediteeFile(final String path) throws IOException { 48 finalBufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(path), "UTF-8"));49 finalString firstLine = in.readLine();52 BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(path), "UTF-8")); 53 String firstLine = in.readLine(); 50 54 in.close(); 55 if (firstLine == null) return false; 51 56 return firstLine.startsWith(ENCRYPTED_EXP_FLAG); 52 57 } … … 87 92 if (accessDenied) { return null; } 88 93 94 _readingSurrogates = false; 89 95 _reader = reader; 90 96 String next = ""; … … 106 112 next = readTheConstraints(); 107 113 114 if (next.equals(EncryptedExpWriter.TERMINATOR_WITH_CONTINUATION)) { 115 // Read the surrogates 116 _readingSurrogates = true; 117 next = readTheItems(newFrame, delayedActions); 118 _readingSurrogates = false; 119 } 120 108 121 for(DelayedAction action: delayedActions) { 109 122 action.exec(); … … 124 137 } 125 138 126 // @Override127 // protected String readTheItems(Frame newFrame, List<DelayedAction> delayedActions) throws IOException {128 // String next = null;129 // Item currentItem = null;130 //131 // Predicate<String> endOfSection = s -> s.equals(EncryptedExpWriter.TERMINATOR + "") || s.equals(EncryptedExpWriter.TERMINATOR_WITH_CONTINUATION);132 // while (_reader.ready() && !endOfSection.test(next = _reader.readLine())) {133 // if (!isValidLine(next)) {134 // continue;135 // }136 //137 // String tag = getTagEnc(next);138 // if (next.startsWith(DefaultFrameWriter.TYPE_AND_ID_STR + " ")) {139 // currentItem = newItem(next);140 // _linePoints.put(currentItem.getID(), currentItem);141 // newFrame.addItem(currentItem);142 // currentItem.setEncryptionDetailForTag(tag + "", EncryptionDetail.UnencryptedOnSave);143 // } else if (next.startsWith("SurrogateFor")) {144 // int parentID = Integer.parseInt(next.split(" ")[1]);145 // newFrame.getItemWithID(parentID).addToSurrogates(currentItem);146 // newFrame.addToSurrogates(currentItem);147 // } else if (currentItem != null && actionShouldBeDelayed(tag.charAt(0))) {148 // delayedActions.add(new DelayedAction(currentItem, next));149 // } else if (currentItem != null) {150 // processBodyLine(currentItem, next);151 // } else {152 // System.err.println("Error while reading in frame (ExpReader): Found body line but no current item to apply it to.");153 // }154 // }155 //156 // if (next.equals(EncryptedExpWriter.TERMINATOR_WITH_CONTINUATION)) {157 // next = readTheItems(newFrame, delayedActions);158 // }159 //160 // return next;161 // }162 163 139 protected String readTheItems(Frame newFrame, List<DelayedAction> delayedActions) throws IOException { 164 // Read the primary item. 165 BiConsumer<Item, String> addToBody = (item, line) -> newFrame.addItem(item); 166 String next = readLineAfterLine(false, addToBody, delayedActions); 167 168 // Read the surrogate items. 169 if (next.equals(EncryptedExpWriter.TERMINATOR_WITH_CONTINUATION)) { 170 BiConsumer<Item, String> addToSurrogates = (item, line) -> { 171 int parentID = Integer.parseInt(line.split(" ")[1]); 172 newFrame.getItemWithID(parentID).addToSurrogates(item); 173 newFrame.addToSurrogates(item); 174 }; 175 next = readLineAfterLine(true, addToSurrogates, delayedActions); 140 BiConsumer<Item, String> primaryAdd = (item, line) -> newFrame.addItem(item); 141 BiConsumer<Item, String> surrogateAdd = (item, line) -> { 142 int parentID = Integer.parseInt(line.split(" ")[1]); 143 Item parent = newFrame.getItemWithID(parentID); 144 newFrame.addToSurrogatesOnLoad(item, parent); 145 }; 146 147 if (_readingSurrogates) { 148 return readLineAfterLine(true, surrogateAdd, delayedActions); 149 } else { 150 return readLineAfterLine(false, primaryAdd, delayedActions); 151 } 152 } 153 154 @Override 155 protected String readTheConstraints() throws IOException, Exception { 156 String next = null; 157 while (_reader.ready() && !endOfSection.test(next = _reader.readLine())) { 158 if (isValidLine(next)) { 159 Point idtype = separateValues(next.substring(2)); 160 // The next line must be the endpoints 161 if (!_reader.ready()) { 162 throw new Exception("Unexpected end of file"); 163 } 164 next = _reader.readLine(); 165 Point startend = separateValues(next.substring(2)); 166 167 Item a = _linePoints.get(startend.getX()); 168 Item b = _linePoints.get(startend.getY()); 169 170 new Constraint(a, b, idtype.getX(), idtype.getY()); 171 } 176 172 } 177 173 return next; … … 182 178 Item currentItem = null; 183 179 184 Predicate<String> endOfSection = s -> s.equals(EncryptedExpWriter.TERMINATOR + "") || s.equals(EncryptedExpWriter.TERMINATOR_WITH_CONTINUATION);185 180 while (_reader.ready() && !endOfSection.test(next = _reader.readLine())) { 186 181 if (!isValidLine(next)) { … … 195 190 storeResult.accept(currentItem, next); 196 191 } 197 currentItem.setEncryptionDetailForTag(tag + "", EncryptionDetail.UnencryptedOnSave); 192 EncryptionDetail unencryptedOnSave = new EncryptionDetail(EncryptionDetail.Type.UnencryptedOnSave); 193 currentItem.setEncryptionDetailForTag(tag + "", unencryptedOnSave); 198 194 } else if (next.startsWith("SurrogateFor")) { 199 195 if (isSurrogate) { … … 212 208 } 213 209 214 // private String readLineAfterLine(Consumer<Item> storeResult, List<DelayedAction> delayedActions) throws IOException {215 // String next = null;216 // Item currentItem = null;217 //218 // Predicate<String> endOfSection = s -> {219 // return s.equals(EncryptedExpWriter.TERMINATOR + "") || s.equals(EncryptedExpWriter.TERMINATOR_WITH_CONTINUATION);220 // };221 // while (_reader.ready() && !endOfSection.test(next = _reader.readLine())) {222 // if (!isValidLine(next)) {223 // continue;224 // }225 //226 // String tag = getTagEnc(next);227 // if (next.startsWith(DefaultFrameWriter.TYPE_AND_ID_STR + " ")) {228 // currentItem = newItem(next);229 // _linePoints.put(currentItem.getID(), currentItem);230 // currentItem.setEncryptionDetailForTag(tag + "", EncryptionDetail.UnencryptedOnSave);231 // storeResult.accept(currentItem);232 // } else if (next.startsWith("SurrogateFor")) {233 // int parentID = Integer.parseInt(next.split(" ")[1]);234 // newFrame.getItemWithID(parentID).addToSurrogates(currentItem);235 // new Delayed236 // }237 // }238 //239 // return next;240 // }241 242 210 @Override 243 211 protected void processBodyLine(Item item, String line) { … … 245 213 String tag = getTagEnc(line); 246 214 String value = getValue(line); 247 248 if (item.isSurrogate() && item.isTagInherited(tag)) { 215 boolean isEncryptedLine = isEncryptedLine(line); 216 217 if (item.isSurrogate() && isEncryptedLine) { 218 // Surrogates should never have encrypted body lines. 249 219 return; 250 220 } 251 221 252 222 // Attempt to decrypt the line if necessary. 253 if (isEncryptedLine (line)) {254 LabelResult res = Label. resolveKey(item.getEncryptionLabel());223 if (isEncryptedLine) { 224 LabelResult res = Label.getLabel(item.getEncryptionLabel()); 255 225 if (res == LabelResult.SuccessResolveLabelToKey) { 256 item.setEncryptionDetailForTag(tag, EncryptionDetail.ReencryptOnSave); 226 EncryptionDetail reencryptOnSave = new EncryptionDetail(EncryptionDetail.Type.ReencryptOnSave); 227 item.setEncryptionDetailForTag(tag, reencryptOnSave); 257 228 SecretKey key = new SecretKeySpec(res.key, SymmetricAlgorithm); 258 229 byte[] decryptedBytes = DecryptSymmetric(Base64.getDecoder().decode(value), key); 259 230 value = new String(decryptedBytes); 260 231 } else { 261 EncryptionDetail encryptionDetail = EncryptionDetail.UseUndecipheredValueOnSave.setUndecipheredValue(getValue(line)); 262 item.setEncryptionDetailForTag(tag, encryptionDetail); 232 EncryptionDetail undecipheredValueOnSave = new EncryptionDetail(EncryptionDetail.Type.UseUndecipheredValueOnSave); 233 undecipheredValueOnSave.setUndecipheredValue(getValue(line)); 234 item.setEncryptionDetailForTag(tag, undecipheredValueOnSave); 263 235 return; 264 236 } 265 237 } else { 266 item.setEncryptionDetailForTag(tag, EncryptionDetail.UnencryptedOnSave); 238 EncryptionDetail unencryptedOnSave = new EncryptionDetail(EncryptionDetail.Type.UnencryptedOnSave); 239 item.setEncryptionDetailForTag(tag, unencryptedOnSave); 240 if (item.isSurrogate()) { 241 item.setTagNotInherited(tag); 242 } 267 243 } 268 244 … … 394 370 395 371 private SecretKeySpec resolveLabel(String label) { 396 LabelResult res = Label. resolveKey(label);372 LabelResult res = Label.getLabel(label); 397 373 if (res == LabelResult.SuccessResolveLabelToKey) { 398 374 byte[] keyBytes = res.key;
Note:
See TracChangeset
for help on using the changeset viewer.