Skip to content
Snippets Groups Projects
Commit ab0ab53d authored by Thai Duong's avatar Thai Duong
Browse files

Temporarily remove the EnvelopeMe example.

Reason: this example is deprecated because it's using a set of APIs
that are going through heavy refactoring. I've written a new example,
but it needs to wait until the refactoring is done.

Change-Id: I78187c6e713f521f7695130ba846469ac69bcf9e
ORIGINAL_AUTHOR=Thai Duong <thaidn@google.com>
GitOrigin-RevId: 4153a2bf758e9afd2fc081b5d28dfc81d7f5bc31
parent 1696b8ca
No related branches found
No related tags found
No related merge requests found
licenses(["notice"]) # Apache 2.0
load("//java/build_defs:javac.bzl", "JAVACOPTS")
java_binary(
name = "envelopeme",
srcs = glob([
"src/main/java/com/example/envelopeme/*.java",
]),
javacopts = JAVACOPTS,
main_class = "com.example.envelopeme.EnvelopeMe",
deps = [
"//java",
"//java:subtle",
"@args4j//jar",
],
)
/*
* Copyright (c) 2017 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package com.example.envelopeme;
import com.google.crypto.tink.Aead;
import com.google.crypto.tink.KeysetHandle;
import com.google.crypto.tink.KeysetReaders;
import com.google.crypto.tink.NoSecretKeysetHandle;
import com.google.crypto.tink.aead.AeadConfig;
import com.google.crypto.tink.aead.AeadFactory;
import com.google.crypto.tink.mac.MacConfig;
import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;
/**
* Implements the commands specified in {@code EnvelopeMeCommands}.
*/
public class EnvelopeMe {
/**
* Encrypts the given bytes, using the key config file and the credential file.
*/
public static byte[] encrypt(byte[] config, byte[] plaintext)
throws Exception {
KeysetHandle handle = NoSecretKeysetHandle.read(
KeysetReaders.withBytes(config));
Aead aead = AeadFactory.getPrimitive(handle);
return aead.encrypt(plaintext, /* additionalData= */null);
}
/**
* Decrypts the given encrypted bytes, using the key config file and the credential file.
*/
public static byte[] decrypt(byte[] config, byte[] ciphertext)
throws Exception {
KeysetHandle handle = NoSecretKeysetHandle.read(
KeysetReaders.withBytes(config));
Aead aead = AeadFactory.getPrimitive(handle);
return aead.decrypt(ciphertext, /* additionalData= */null);
}
public static void main(String[] args) throws Exception {
AeadConfig.registerStandardKeyTypes();
MacConfig.registerStandardKeyTypes();
EnvelopeMeCommands commands = new EnvelopeMeCommands();
CmdLineParser parser = new CmdLineParser(commands);
try {
parser.parseArgument(args);
} catch (CmdLineException e) {
System.out.println(e);
System.out.println();
e.getParser().printUsage(System.out);
System.exit(1);
}
commands.command.run();
}
}
/*
* Copyright (c) 2017 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package com.example.envelopeme;
import com.google.crypto.tink.Registry;
import com.google.crypto.tink.aead.KmsAeadKeyManager;
import com.google.crypto.tink.integration.CloudKmsClient;
import com.google.crypto.tink.integration.GcpKmsClient;
import com.google.crypto.tink.subtle.SubtleUtil;
import java.io.File;
import java.io.FileOutputStream;
import java.nio.file.Files;
import org.kohsuke.args4j.Argument;
import org.kohsuke.args4j.Option;
import org.kohsuke.args4j.spi.SubCommand;
import org.kohsuke.args4j.spi.SubCommandHandler;
import org.kohsuke.args4j.spi.SubCommands;
/**
* Defines the different sub-commands and their parameters, for command-line invocation.
*/
class EnvelopeMeCommands {
/**
* An interface for a command-line sub-command.
*/
interface Command {
public void run() throws Exception;
}
// Most of the commands take some subset of the same arguments, so specify groups of arguments
// as classes for greater code reuse.
static class Args {
@Option(name = "--config", required = true, usage = "The key config file")
File configFile;
@Option(name = "--credential", required = true, usage = "The credential file")
File credentialFile;
@Argument(metaVar = "inFile", required = true, index = 0, usage = "The source file")
File inFile;
@Argument(metaVar = "outFile", required = true, index = 1, usage = "The destination file")
File outFile;
void validate() {
try {
SubtleUtil.validateNotExists(outFile);
SubtleUtil.validateExists(configFile);
SubtleUtil.validateExists(credentialFile);
SubtleUtil.validateExists(inFile);
} catch (Exception e) {
SubtleUtil.die(e.toString());
}
}
}
public static class EncryptCommand extends Args implements Command {
@Override
public void run() throws Exception {
Registry.INSTANCE.registerKeyManager(
"type.googleapis.com/google.crypto.tink.KmsAeadKey",
new KmsAeadKeyManager(
new CloudKmsClient()
.withGcpKmsClient(GcpKmsClient.fromServiceAccount(credentialFile))));
byte[] encrypted = EnvelopeMe.encrypt(
Files.readAllBytes(configFile.toPath()),
Files.readAllBytes(inFile.toPath()));
FileOutputStream stream = new FileOutputStream(outFile);
try {
stream.write(encrypted);
} finally {
stream.close();
}
}
}
public static class DecryptCommand extends Args implements Command {
@Override
public void run() throws Exception {
Registry.INSTANCE.registerKeyManager(
"type.googleapis.com/google.crypto.tink.KmsAeadKey",
new KmsAeadKeyManager(
new CloudKmsClient()
.withGcpKmsClient(GcpKmsClient.fromServiceAccount(credentialFile))));
byte[] decrypted = EnvelopeMe.decrypt(
Files.readAllBytes(configFile.toPath()),
Files.readAllBytes(inFile.toPath()));
FileOutputStream stream = new FileOutputStream(outFile);
try {
stream.write(decrypted);
} finally {
stream.close();
}
}
}
@Argument(metaVar = "command", required = true, handler = SubCommandHandler.class,
usage = "The subcommand to run")
@SubCommands({
@SubCommand(name = "encrypt", impl = EncryptCommand.class),
@SubCommand(name = "decrypt", impl = DecryptCommand.class)
})
Command command;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment