|
1 | 1 | package io.opentdf.platform;
|
2 | 2 |
|
3 |
| -import io.opentdf.platform.sdk.Config; |
4 |
| -import io.opentdf.platform.sdk.SDK; |
5 |
| -import io.opentdf.platform.sdk.SDKBuilder; |
| 3 | +import io.opentdf.platform.sdk.*; |
6 | 4 | import io.opentdf.platform.sdk.TDF;
|
7 | 5 | import picocli.CommandLine;
|
8 | 6 | import picocli.CommandLine.Option;
|
|
18 | 16 | import java.io.IOException;
|
19 | 17 | import java.io.PrintWriter;
|
20 | 18 | import java.io.StringWriter;
|
| 19 | +import java.nio.ByteBuffer; |
21 | 20 | import java.nio.channels.FileChannel;
|
22 | 21 | import java.nio.file.Path;
|
23 | 22 | import java.nio.file.StandardOpenOption;
|
@@ -98,4 +97,43 @@ void readMetadata(@Option(names = {"-f", "--file"}, required = true) Path tdfPat
|
98 | 97 | }
|
99 | 98 | }
|
100 | 99 | }
|
| 100 | + |
| 101 | + @CommandLine.Command(name = "encryptnano") |
| 102 | + void createNanoTDF( |
| 103 | + @Option(names = {"-f", "--file"}, defaultValue = Option.NULL_VALUE) Optional<File> file, |
| 104 | + @Option(names = {"-k", "--kas-url"}, required = true) List<String> kas, |
| 105 | + @Option(names = {"-m", "--metadata"}, defaultValue = Option.NULL_VALUE) Optional<String> metadata) throws Exception { |
| 106 | + |
| 107 | + var sdk = buildSDK(); |
| 108 | + var kasInfos = kas.stream().map(k -> { |
| 109 | + var ki = new Config.KASInfo(); |
| 110 | + ki.URL = k; |
| 111 | + return ki; |
| 112 | + }).toArray(Config.KASInfo[]::new); |
| 113 | + |
| 114 | + List<Consumer<Config.NanoTDFConfig>> configs = new ArrayList<>(); |
| 115 | + configs.add(Config.withNanoKasInformation(kasInfos)); |
| 116 | + |
| 117 | + var nanoTDFConfig = Config.newNanoTDFConfig(configs.toArray(Consumer[]::new)); |
| 118 | + try (var in = file.isEmpty() ? new BufferedInputStream(System.in) : new FileInputStream(file.get())) { |
| 119 | + try (var out = new BufferedOutputStream(System.out)) { |
| 120 | + NanoTDF ntdf = new NanoTDF(); |
| 121 | + ntdf.createNanoTDF(ByteBuffer.wrap(in.readAllBytes()), out, nanoTDFConfig, sdk.getServices().kas()); |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + @CommandLine.Command(name = "decryptnano") |
| 127 | + void readNanoTDF(@Option(names = {"-f", "--file"}, required = true) Path nanoTDFPath) throws Exception { |
| 128 | + var sdk = buildSDK(); |
| 129 | + try (var in = FileChannel.open(nanoTDFPath, StandardOpenOption.READ)) { |
| 130 | + try (var stdout = new BufferedOutputStream(System.out)) { |
| 131 | + NanoTDF ntdf = new NanoTDF(); |
| 132 | + ByteBuffer buffer = ByteBuffer.allocate((int) in.size()); |
| 133 | + in.read(buffer); |
| 134 | + buffer.flip(); |
| 135 | + ntdf.readNanoTDF(buffer, stdout, sdk.getServices().kas()); |
| 136 | + } |
| 137 | + } |
| 138 | + } |
101 | 139 | }
|
0 commit comments