use of build.bazel.remote.execution.v2.Digest in project java-docs-samples by GoogleCloudPlatform.
the class SnippetsIT method testVerifyAsymmetricRsa.
@Test
public void testVerifyAsymmetricRsa() throws IOException, GeneralSecurityException {
String message = "my message";
byte[] signature;
try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {
CryptoKeyVersionName versionName = CryptoKeyVersionName.of(PROJECT_ID, LOCATION_ID, KEY_RING_ID, ASYMMETRIC_SIGN_RSA_KEY_ID, "1");
MessageDigest sha256 = MessageDigest.getInstance("SHA-256");
byte[] hash = sha256.digest(message.getBytes(StandardCharsets.UTF_8));
Digest digest = Digest.newBuilder().setSha256(ByteString.copyFrom(hash)).build();
signature = client.asymmetricSign(versionName, digest).getSignature().toByteArray();
}
new VerifyAsymmetricRsa().verifyAsymmetricRsa(PROJECT_ID, LOCATION_ID, KEY_RING_ID, ASYMMETRIC_SIGN_RSA_KEY_ID, "1", message, signature);
assertThat(stdOut.toString()).contains("Signature");
}
use of build.bazel.remote.execution.v2.Digest in project java-kms by googleapis.
the class SignAsymmetric method signAsymmetric.
// Get the public key associated with an asymmetric key.
public void signAsymmetric(String projectId, String locationId, String keyRingId, String keyId, String keyVersionId, String message) throws IOException, GeneralSecurityException {
// safely clean up any remaining background resources.
try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {
// Build the key version name from the project, location, key ring, key,
// and key version.
CryptoKeyVersionName keyVersionName = CryptoKeyVersionName.of(projectId, locationId, keyRingId, keyId, keyVersionId);
// Convert the message into bytes. Cryptographic plaintexts and
// ciphertexts are always byte arrays.
byte[] plaintext = message.getBytes(StandardCharsets.UTF_8);
// Calculate the digest.
MessageDigest sha256 = MessageDigest.getInstance("SHA-256");
byte[] hash = sha256.digest(plaintext);
// Build the digest object.
Digest digest = Digest.newBuilder().setSha256(ByteString.copyFrom(hash)).build();
// Sign the digest.
AsymmetricSignResponse result = client.asymmetricSign(keyVersionName, digest);
// Get the signature.
byte[] signature = result.getSignature().toByteArray();
System.out.printf("Signature %s%n", signature);
}
}
use of build.bazel.remote.execution.v2.Digest in project java-kms by googleapis.
the class SnippetsIT method testVerifyAsymmetricEc.
@Test
public void testVerifyAsymmetricEc() throws IOException, GeneralSecurityException {
String message = "my message";
byte[] signature;
try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {
CryptoKeyVersionName versionName = CryptoKeyVersionName.of(PROJECT_ID, LOCATION_ID, KEY_RING_ID, ASYMMETRIC_SIGN_EC_KEY_ID, "1");
MessageDigest sha256 = MessageDigest.getInstance("SHA-256");
byte[] hash = sha256.digest(message.getBytes(StandardCharsets.UTF_8));
Digest digest = Digest.newBuilder().setSha256(ByteString.copyFrom(hash)).build();
signature = client.asymmetricSign(versionName, digest).getSignature().toByteArray();
}
new VerifyAsymmetricEc().verifyAsymmetricEc(PROJECT_ID, LOCATION_ID, KEY_RING_ID, ASYMMETRIC_SIGN_EC_KEY_ID, "1", message, signature);
assertThat(stdOut.toString()).contains("Signature");
}
use of build.bazel.remote.execution.v2.Digest in project java-kms by googleapis.
the class SnippetsIT method testVerifyAsymmetricRsa.
@Test
public void testVerifyAsymmetricRsa() throws IOException, GeneralSecurityException {
String message = "my message";
byte[] signature;
try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {
CryptoKeyVersionName versionName = CryptoKeyVersionName.of(PROJECT_ID, LOCATION_ID, KEY_RING_ID, ASYMMETRIC_SIGN_RSA_KEY_ID, "1");
MessageDigest sha256 = MessageDigest.getInstance("SHA-256");
byte[] hash = sha256.digest(message.getBytes(StandardCharsets.UTF_8));
Digest digest = Digest.newBuilder().setSha256(ByteString.copyFrom(hash)).build();
signature = client.asymmetricSign(versionName, digest).getSignature().toByteArray();
}
new VerifyAsymmetricRsa().verifyAsymmetricRsa(PROJECT_ID, LOCATION_ID, KEY_RING_ID, ASYMMETRIC_SIGN_RSA_KEY_ID, "1", message, signature);
assertThat(stdOut.toString()).contains("Signature");
}
use of build.bazel.remote.execution.v2.Digest in project bazel-buildfarm by bazelbuild.
the class ShardInstance method findMissingBlobsOnWorker.
private void findMissingBlobsOnWorker(String requestId, Iterable<Digest> blobDigests, Deque<String> workers, ImmutableList.Builder<FindMissingResponseEntry> responses, int originalSize, Executor executor, SettableFuture<Iterable<Digest>> missingDigestsFuture, RequestMetadata requestMetadata) {
String worker = workers.removeFirst();
ListenableFuture<Iterable<Digest>> workerMissingBlobsFuture = workerStub(worker).findMissingBlobs(blobDigests, requestMetadata);
Stopwatch stopwatch = Stopwatch.createStarted();
addCallback(workerMissingBlobsFuture, new FutureCallback<Iterable<Digest>>() {
@Override
public void onSuccess(Iterable<Digest> missingDigests) {
if (Iterables.isEmpty(missingDigests) || workers.isEmpty()) {
missingDigestsFuture.set(missingDigests);
} else {
responses.add(new FindMissingResponseEntry(worker, stopwatch.elapsed(MICROSECONDS), null, Iterables.size(missingDigests)));
findMissingBlobsOnWorker(requestId, missingDigests, workers, responses, originalSize, executor, missingDigestsFuture, requestMetadata);
}
}
@Override
public void onFailure(Throwable t) {
responses.add(new FindMissingResponseEntry(worker, stopwatch.elapsed(MICROSECONDS), t, Iterables.size(blobDigests)));
Status status = Status.fromThrowable(t);
if (status.getCode() == Code.UNAVAILABLE || status.getCode() == Code.UNIMPLEMENTED) {
removeMalfunctioningWorker(worker, t, "findMissingBlobs(" + requestId + ")");
} else if (status.getCode() == Code.DEADLINE_EXCEEDED) {
for (FindMissingResponseEntry response : responses.build()) {
logger.log(response.exception == null ? Level.WARNING : Level.SEVERE, format("DEADLINE_EXCEEDED: findMissingBlobs(%s) %s: %d remaining of %d %dus%s", requestId, response.worker, response.stillMissingAfter, originalSize, response.elapsedMicros, response.exception != null ? ": " + response.exception.toString() : ""));
}
missingDigestsFuture.setException(status.asException());
} else if (status.getCode() == Code.CANCELLED || Context.current().isCancelled() || !SHARD_IS_RETRIABLE.test(status)) {
// do nothing further if we're cancelled
missingDigestsFuture.setException(status.asException());
} else {
// why not, always
workers.addLast(worker);
}
if (!missingDigestsFuture.isDone()) {
if (workers.isEmpty()) {
missingDigestsFuture.set(blobDigests);
} else {
findMissingBlobsOnWorker(requestId, blobDigests, workers, responses, originalSize, executor, missingDigestsFuture, requestMetadata);
}
}
}
}, executor);
}
Aggregations