设置 Tink Java

安装并设置 Tink 后,请继续执行后续步骤

Tink Java

核心 Java 库是 tink-java,最新版本为 1.16.0。Tink Java 支持 Java 8 或更高版本。

Maven

您可以使用 Maven 添加 Tink Java:

<dependency>
  <groupId>com.google.crypto.tink</groupId>
  <artifactId>tink</artifactId>
  <version>1.16.0</version>
</dependency>

Bazel

Maven 版本工件

Bazel 用户将 Tink Java 添加为依赖项的推荐方法是在 WORKSPACE 文件中使用 rules_jvm_external 工具安装 Maven 版本工件:

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

RULES_JVM_EXTERNAL_TAG = "6.1"
RULES_JVM_EXTERNAL_SHA ="d31e369b854322ca5098ea12c69d7175ded971435e55c18dd9dd5f29cc5249ac"

http_archive(
    name = "rules_jvm_external",
    strip_prefix = "rules_jvm_external-%s" % RULES_JVM_EXTERNAL_TAG,
    sha256 = RULES_JVM_EXTERNAL_SHA,
    url = "https://github.com/bazelbuild/rules_jvm_external/releases/download/%s/rules_jvm_external-%s.tar.gz" % (RULES_JVM_EXTERNAL_TAG, RULES_JVM_EXTERNAL_TAG)
)

load("@rules_jvm_external//:repositories.bzl", "rules_jvm_external_deps")

rules_jvm_external_deps()

load("@rules_jvm_external//:setup.bzl", "rules_jvm_external_setup")

rules_jvm_external_setup()

load("@rules_jvm_external//:defs.bzl", "maven_install")

maven_install(
    artifacts = [
        "com.google.crypto.tink:tink:1.16.0",
        # ... other dependencies ...
    ],
    repositories = [
        "https://maven.google.com",
        "https://repo1.maven.org/maven2",
    ],
)

从源代码构建

如果您想从源代码构建 tink-java(例如,固定特定提交),可以将其作为 http_archive 包含在 WORKSPACE 文件中:

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

TINK_COMMIT="f4127f6b6ab9c367d41ade1f50db6f0ef9909044"
TINK_SHA256="e246f848f7749e37f558955ecb50345b04d79ddb9d8d1e8ae19f61e8de530582"

http_archive(
    name = "tink_java",
    urls = ["https://github.com/tink-crypto/tink-java/archive/%s.zip" % TINK_COMMIT],
    strip_prefix = "tink-%s" % TINK_COMMIT,
    sha256 = TINK_SHA256
)

load("@tink_java//:tink_java_deps.bzl", "TINK_MAVEN_ARTIFACTS", "tink_java_deps")

tink_java_deps()

load("@tink_java//:tink_java_deps_init.bzl", "tink_java_deps_init")

tink_java_deps_init()

# rules_jvm_external is imported and initialized by tink_java_deps and
# tink_java_deps_init.
load("@rules_jvm_external//:defs.bzl", "maven_install")

maven_install(
  artifacts = TINK_MAVEN_ARTIFACTS + # ... other dependencies ...
  repositories = [
      "https://maven.google.com",
      "https://repo1.maven.org/maven2",
  ],
)

Tink Android

核心 Android 库是 tink-java,最新版本为 1.16.0

从 API 级别 24 开始,完全支持 Tink Android。从 API 级别 21 开始,Tink 的大部分部分都应该可以正常运行。在 API 级别 21 下,Tink 无法开箱即用,需要进行以下配置:

  • JWT 库需要 API 级别 24,因为它使用 java.util.Optional 等类。通过脱糖可以避免此限制。

  • 从 API 级别 23 开始,com.google.crypto.tink.integration.android 中的类才会经过全面测试。

  • com.google.crypto.tink.streamingaead 中的某些 API 使用 SeekableByteBufferChannel,而 SeekableByteBufferChannel 仅从 API 级别 24 开始提供。

出于技术原因,我们仅在 Google 内部基础架构上测试 Android 版 Tink。我们预计不会因此出现任何问题,但如果您遇到任何问题,请提交问题。

Tink Android 无需 proguard 配置。

Gradle

您可以使用 Gradle 中的 tink-android

dependencies {
  implementation 'com.google.crypto.tink:tink-android:1.16.0'
}

AWS KMS 扩展

Tink Java AWS KMS 扩展程序为 tink-java-awskms,最新版本为 1.11.0

Maven

您可以使用 Maven 添加 Tink Java AWS KMS 扩展程序:

<dependencies>
  <dependency>
    <groupId>com.google.crypto.tink</groupId>
    <artifactId>tink-awskms</artifactId>
    <version>1.11.0</version>
  </dependency>
</dependencies>

Bazel

Maven 版本工件

您可以使用 rules_jvm_external 工具将 com.google.crypto.tink:tink-awskms Maven 工件与 com.google.crypto.tink:tink 一起安装。

# ...

maven_install(
    artifacts = [
        "com.google.crypto.tink:tink:1.16.0",
        "com.google.crypto.tink:tink-awskms:1.11.0",
        # ... other dependencies ...
    ],
    repositories = [
        "https://maven.google.com",
        "https://repo1.maven.org/maven2",
    ],
)

从源代码构建

如果您想从源代码构建 tink-awskms(例如固定特定提交),可以将其作为 http_archive 添加到 WORKSPACE 文件中:

# ...

http_archive(
    name = "tink_java",
    urls = ["https://github.com/tink-crypto/tink-java/releases/download/v1.16.0/tink-java-1.16.0.zip"],
    strip_prefix = "tink-java-1.16.0",
    sha256 = "6bf0bb13281257fdf07d70abfc025f0e3ab18abd22646b1ada3fe297f7feaedb",
)

load("@tink_java//:tink_java_deps.bzl", "TINK_MAVEN_ARTIFACTS", "tink_java_deps")

tink_java_deps()

load("@tink_java//:tink_java_deps_init.bzl", "tink_java_deps_init")

tink_java_deps_init()

http_archive(
    name = "tink_java_awskms",
    urls = ["https://github.com/tink-crypto/tink-java-awskms/releases/download/v1.11.0/tink-java-awskms-1.11.0.zip"],
    strip_prefix = "tink-java-awskms-1.11.0",
    sha256 = "18f8faa7ba0019fc584f9e03f94221ebbcc83f059568d2277a4866003153e151",
)

load("@tink_java_awskms//:tink_java_awskms_deps.bzl", "TINK_JAVA_AWSKMS_MAVEN_ARTIFACTS")

maven_install(
    artifacts = TINK_MAVEN_ARTIFACTS + TINK_JAVA_AWSKMS_MAVEN_ARTIFACTS + [
        # ... other dependencies ...
    ],
    repositories = [
        "https://maven.google.com",
        "https://repo1.maven.org/maven2",
    ],
)

Google Cloud KMS 扩展程序

Tink Java Google Cloud KMS 扩展程序是 tink-java-gcpkms,最新版本为 1.10.0

Maven

您可以使用 Maven 添加 Tink Java Google Cloud KMS 扩展程序:

<dependencies>
  <dependency>
    <groupId>com.google.crypto.tink</groupId>
    <artifactId>tink-gcpkms</artifactId>
    <version>1.10.0/version>
  </dependency>
</dependencies>

Bazel

Maven 版本工件

您可以使用 rules_jvm_external 工具将 com.google.crypto.tink:tink-gcpkms Maven 工件与 com.google.crypto.tink:tink 一起安装。

# ...

maven_install(
    artifacts = [
        "com.google.crypto.tink:tink:1.16.0",
        "com.google.crypto.tink:tink-gcpkms:1.10.0",
        # ... other dependencies ...
    ],
    repositories = [
        "https://maven.google.com",
        "https://repo1.maven.org/maven2",
    ],
)

从源代码构建

如果您想从源代码构建 tink-gcpkms(例如固定特定提交),可以将其作为 http_archive 添加到 WORKSPACE 文件中:

# ...

http_archive(
    name = "tink_java",
    urls = ["https://github.com/tink-crypto/tink-java/releases/download/v1.16.0/tink-java-1.16.0.zip"],
    strip_prefix = "tink-java-1.16.0",
    sha256 = "6bf0bb13281257fdf07d70abfc025f0e3ab18abd22646b1ada3fe297f7feaedb",
)

load("@tink_java//:tink_java_deps.bzl", "TINK_MAVEN_ARTIFACTS", "tink_java_deps")

tink_java_deps()

load("@tink_java//:tink_java_deps_init.bzl", "tink_java_deps_init")

tink_java_deps_init()

http_archive(
    name = "tink_java_gcpkms",
    urls = ["https://github.com/tink-crypto/tink-java-gcpkms/releases/download/v1.10.0/tink-java-gcpkms-1.10.0.zip"],
    strip_prefix = "tink-java-gcpkms-1.10.0",
    sha256 = "ad85625cc4409f2f6ab13a8eef39c965501585e9323d59652cce322b3d2c09a2",
)

load("@tink_java_gcpkms//:tink_java_gcpkms_deps.bzl", "TINK_JAVA_GCPKMS_MAVEN_ARTIFACTS")

maven_install(
    artifacts =  TINK_MAVEN_ARTIFACTS + TINK_JAVA_GCPKMS_MAVEN_ARTIFACTS + [
        # ... other dependencies ...
    ],
    repositories = [
        "https://maven.google.com",
        "https://repo1.maven.org/maven2",
    ],
)

Tink Java Apps

Tink Java Apps 库提供了 Google 付款方式令牌Google AdMob 激励广告的服务器端验证RFC 8291 - Web Push 消息加密的实现,最新版本为 1.11.0

Maven

您可以使用 Maven 添加 Tink Java Apps 库:

<dependency>
  <groupId>com.google.crypto.tink</groupId>
  <artifactId>apps-webpush</artifactId>
  <version>1.11.0</version>
</dependency>

<dependency>
  <groupId>com.google.crypto.tink</groupId>
  <artifactId>apps-paymentmethodtoken</artifactId>
  <version>1.11.0</version>
</dependency>

<dependency>
  <groupId>com.google.crypto.tink</groupId>
  <artifactId>apps-rewardedads</artifactId>
  <version>1.11.0</version>
</dependency>

Bazel

您可以使用 rules_jvm_external 工具安装任何 com.google.crypto.tink:apps-* Maven 工件。

# ...

maven_install(
    artifacts = [
        "com.google.crypto.tink:apps-webpush:1.11.0",
        "com.google.crypto.tink:apps-paymentmethodtoken:1.11.0",
        "com.google.crypto.tink:apps-rewardedads:1.11.0",
        # ... other dependencies ...
    ],
    repositories = [
        "https://maven.google.com",
        "https://repo1.maven.org/maven2",
    ],
)

后续步骤

完成 Tink 设置后,请继续执行标准的 Tink 使用步骤:

  • 选择基元 - 根据您的用例确定要使用的基元
  • 管理密钥 - 使用外部 KMS 保护密钥、生成密钥集以及轮替密钥