Skip to content
This repository has been archived by the owner on Apr 17, 2024. It is now read-only.

Commit

Permalink
Reducing usages of java.nio.file which is only avail on Android O or …
Browse files Browse the repository at this point in the history
…newer.

There are still complicated usages in many streaming tests that cannot be
removed.

Also removing org.json as a dependency on the Android flavor, because
Android already provided its own copy of this package.

Change-Id: I48dd57cba51b1dc61451d86ac7c98e14a3beeddf
ORIGINAL_AUTHOR=Thai Duong <thaidn@google.com>
GitOrigin-RevId: 207b0ba544e07b27aa405fe82dbf86aa988c85c6
  • Loading branch information
thaidn committed Sep 19, 2017
1 parent d4665a4 commit ed5cdb2
Show file tree
Hide file tree
Showing 18 changed files with 272 additions and 187 deletions.
5 changes: 3 additions & 2 deletions doc/JAVA-HOWTO.md
Expand Up @@ -21,7 +21,8 @@ To add a dependency on Tink using Maven, use the following:
<artifactId>tink</artifactId>
<version>1.0.0</version>
<!-- or, for Android: -->
<version>1.0.0-android</version>
<artifactId>tink-android</artifactId>
<version>1.0.0</version>
</dependency>
```

Expand All @@ -31,7 +32,7 @@ To add a dependency using Gradle:
dependencies {
compile 'com.google.crypto.tink:tink:1.0.0'
// or, for Android:
compile 'com.google.crypto.tink:tink:1.0.0-android'
compile 'com.google.crypto.tink:tink-android:1.0.0'
}
```

Expand Down
Expand Up @@ -31,6 +31,10 @@
public final class BinaryKeysetReader implements KeysetReader {
private final InputStream inputStream;

/**
* Note: the input stream won't be read until {@link BinaryKeysetReader#read} or
* {@link BinaryKeysetReader#readEncrypted} is called.
*/
public static KeysetReader withInputStream(InputStream stream) {
return new BinaryKeysetReader(stream);
}
Expand All @@ -39,6 +43,10 @@ public static KeysetReader withBytes(final byte[] bytes) {
return new BinaryKeysetReader(new ByteArrayInputStream(bytes));
}

/**
* Note: the input file won't be read until {@link BinaryKeysetReader#read} or
* {@link BinaryKeysetReader#readEncrypted} is called.
*/
public static KeysetReader withFile(File file) throws IOException {
return new BinaryKeysetReader(new FileInputStream(file));
}
Expand Down
77 changes: 48 additions & 29 deletions java/src/main/java/com/google/crypto/tink/JsonKeysetReader.java
Expand Up @@ -29,11 +29,11 @@
import com.google.crypto.tink.proto.OutputPrefixType;
import com.google.crypto.tink.subtle.Base64;
import com.google.protobuf.ByteString;
import java.io.ByteArrayOutputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import org.json.JSONArray;
import org.json.JSONException;
Expand All @@ -44,64 +44,78 @@
* JSON format.
*/
public final class JsonKeysetReader implements KeysetReader {
private final InputStream inputStream;
private final JSONObject json;
private boolean urlSafeBase64 = false;

private JsonKeysetReader(JSONObject input) {
json = input;
private JsonKeysetReader(InputStream inputStream) {
this.inputStream = inputStream;
json = null;
}

private JsonKeysetReader(String input) {
try {
json = new JSONObject(input);
} catch (JSONException e) {
throw new IllegalArgumentException(e);
}
private JsonKeysetReader(JSONObject json) {
this.json = json;
this.inputStream = null;
}

/**
* Note: the input stream won't be read until {@link JsonKeysetReader#read} or
* {@link JsonKeysetReader#readEncrypted} is called.
*/
public static KeysetReader withInputStream(InputStream input) throws IOException {
return new JsonKeysetReader(readAll(input));
return new JsonKeysetReader(input);
}

public static JsonKeysetReader withJsonObject(JSONObject json) {
return new JsonKeysetReader(json);
public static JsonKeysetReader withJsonObject(JSONObject input) {
return new JsonKeysetReader(input);
}

public static JsonKeysetReader withString(String input) {
return new JsonKeysetReader(input);
return new JsonKeysetReader(new ByteArrayInputStream(input.getBytes(UTF_8)));
}

public static JsonKeysetReader withBytes(final byte[] bytes) {
return new JsonKeysetReader(new String(bytes, UTF_8));
return new JsonKeysetReader(new ByteArrayInputStream(bytes));
}

/**
* Note: the file won't be read until {@link JsonKeysetReader#read} or
* {@link JsonKeysetReader#readEncrypted} is called.
*/
public static JsonKeysetReader withFile(File file) throws IOException {
return withPath(file.toPath());
return new JsonKeysetReader(new FileInputStream(file));
}

/**
* Note: the file path won't be read until {@link JsonKeysetReader#read} or
* {@link JsonKeysetReader#readEncrypted} is called.
*/
public static JsonKeysetReader withPath(String path) throws IOException {
return withFile(new File(path));
}

/**
* Note: the file path won't be read until {@link JsonKeysetReader#read} or
* {@link JsonKeysetReader#readEncrypted} is called.
*/
public static JsonKeysetReader withPath(Path path) throws IOException {
return new JsonKeysetReader(new String(Files.readAllBytes(path), UTF_8));
return withFile(path.toFile());
}

public JsonKeysetReader withUrlSafeBase64() {
this.urlSafeBase64 = true;
return this;
}

private static String readAll(InputStream input) throws IOException {
ByteArrayOutputStream result = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int count;
while ((count = input.read(buf)) != -1) {
result.write(buf, 0, count);
}
return result.toString(UTF_8.name());
}

@Override
public Keyset read() throws IOException {
try {
return keysetFromJson(json);
if (json != null) {
return keysetFromJson(json);
} else {
return keysetFromJson(new JSONObject(
new String(Util.readAll(inputStream), UTF_8)));
}
} catch (JSONException e) {
throw new IOException(e);
}
Expand All @@ -110,7 +124,12 @@ public Keyset read() throws IOException {
@Override
public EncryptedKeyset readEncrypted() throws IOException {
try {
return encryptedKeysetFromJson(json);
if (json != null) {
return encryptedKeysetFromJson(json);
} else {
return encryptedKeysetFromJson(new JSONObject(
new String(Util.readAll(inputStream), UTF_8)));
}
} catch (JSONException e) {
throw new IOException(e);
}
Expand Down
Expand Up @@ -53,6 +53,10 @@ public static KeysetWriter withFile(File file) throws IOException {
return new JsonKeysetWriter(new FileOutputStream(file));
}

public static KeysetWriter withPath(String path) throws IOException {
return withFile(new File(path));
}

public static KeysetWriter withPath(Path path) throws IOException {
return withFile(path.toFile());
}
Expand Down
16 changes: 16 additions & 0 deletions java/src/main/java/com/google/crypto/tink/Util.java
Expand Up @@ -21,6 +21,9 @@
import com.google.crypto.tink.proto.Keyset;
import com.google.crypto.tink.proto.KeysetInfo;
import com.google.crypto.tink.proto.OutputPrefixType;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.GeneralSecurityException;

/** Various helpers. */
Expand Down Expand Up @@ -95,4 +98,17 @@ public static void validateKeyset(Keyset keyset) throws GeneralSecurityException
throw new GeneralSecurityException("keyset doesn't contain a valid primary key");
}
}

/**
* Reads all bytes from {@code inputStream}.
*/
public static byte[] readAll(InputStream inputStream) throws IOException {
ByteArrayOutputStream result = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int count;
while ((count = inputStream.read(buf)) != -1) {
result.write(buf, 0, count);
}
return result.toByteArray();
}
}
Expand Up @@ -25,10 +25,9 @@
import com.google.crypto.tink.Aead;
import com.google.crypto.tink.KmsClient;
import com.google.crypto.tink.subtle.Validators;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.GeneralSecurityException;

/** An implementation of {@code KmsClient} for Google Cloud KMS. */
Expand Down Expand Up @@ -80,7 +79,7 @@ public KmsClient withCredentials(String credentialPath) throws GeneralSecurityEx
try {
GoogleCredential credentials =
GoogleCredential.fromStream(
new ByteArrayInputStream(Files.readAllBytes(Paths.get(credentialPath))));
new FileInputStream(new File(credentialPath)));
return withCredentials(credentials);
} catch (IOException e) {
throw new GeneralSecurityException("cannot load credentials", e);
Expand Down
4 changes: 2 additions & 2 deletions java/src/main/java/com/google/crypto/tink/subtle/Ed25519.java
Expand Up @@ -16,13 +16,13 @@

package com.google.crypto.tink.subtle;

import static com.google.crypto.tink.subtle.Field25519.FIELD_LEN;
import static com.google.crypto.tink.subtle.Field25519.LIMB_CNT;
import static com.google.crypto.tink.subtle.Ed25519Constants.B2;
import static com.google.crypto.tink.subtle.Ed25519Constants.B_TABLE;
import static com.google.crypto.tink.subtle.Ed25519Constants.D;
import static com.google.crypto.tink.subtle.Ed25519Constants.D2;
import static com.google.crypto.tink.subtle.Ed25519Constants.SQRTM1;
import static com.google.crypto.tink.subtle.Field25519.FIELD_LEN;
import static com.google.crypto.tink.subtle.Field25519.LIMB_CNT;

import com.google.crypto.tink.annotations.Alpha;
import java.security.GeneralSecurityException;
Expand Down

0 comments on commit ed5cdb2

Please sign in to comment.