<commit_before>// Copyright 2020 Google LLC
//
// 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.
//
///////////////////////////////////////////////////////////////////////////////

// Implementation of an AEAD Service.
#include "aead_impl.h"

#include <string>
#include <utility>

#include "tink/aead.h"
#include "tink/binary_keyset_reader.h"
#include "tink/cleartext_keyset_handle.h"

namespace tink_testing_api {

using ::crypto::tink::BinaryKeysetReader;
using ::crypto::tink::CleartextKeysetHandle;

// Encrypts a message
::grpc::Status AeadImpl::CreateAead(grpc::ServerContext* context,
                                    const AeadCreationRequest* request,
                                    AeadCreationResponse* response) {
  auto reader_result = BinaryKeysetReader::New(request->keyset());
  if (!reader_result.ok()) {
    response->set_err(std::string(reader_result.status().message()));
    return ::grpc::Status::OK;
  }
  auto handle_result =
      CleartextKeysetHandle::Read(std::move(reader_result.value()));
  if (!handle_result.ok()) {
    response->set_err(std::string(handle_result.status().message()));
    return ::grpc::Status::OK;
  }
  auto aead_result = handle_result.value()->GetPrimitive<crypto::tink::Aead>();
  if (!aead_result.ok()) {
    response->set_err(std::string(aead_result.status().message()));
    return ::grpc::Status::OK;
  }
  return ::grpc::Status::OK;
}

::grpc::Status AeadImpl::Encrypt(grpc::ServerContext* context,
                                 const AeadEncryptRequest* request,
                                 AeadEncryptResponse* response) {
  auto reader_result = BinaryKeysetReader::New(request->keyset());
  if (!reader_result.ok()) {
    return ::grpc::Status(::grpc::StatusCode::FAILED_PRECONDITION,
                          "Reading Keyset failed");
  }
  auto handle_result =
      CleartextKeysetHandle::Read(std::move(reader_result.value()));
  if (!handle_result.ok()) {
    return ::grpc::Status(::grpc::StatusCode::FAILED_PRECONDITION,
                          "Creating KeysetHandle failed");
  }
  auto aead_result = handle_result.value()->GetPrimitive<crypto::tink::Aead>();
  if (!aead_result.ok()) {
    return ::grpc::Status(::grpc::StatusCode::FAILED_PRECONDITION,
                          "Creating Aead failed");
  }
  auto encrypt_result = aead_result.value()->Encrypt(
      request->plaintext(), request->associated_data());
  if (!encrypt_result.ok()) {
    response->set_err(std::string(encrypt_result.status().message()));
    return ::grpc::Status::OK;
  }
  response->set_ciphertext(encrypt_result.value());
  return ::grpc::Status::OK;
}

// Decrypts a ciphertext
::grpc::Status AeadImpl::Decrypt(grpc::ServerContext* context,
                                 const AeadDecryptRequest* request,
                                 AeadDecryptResponse* response) {
  auto reader_result = BinaryKeysetReader::New(request->keyset());
  if (!reader_result.ok()) {
    return ::grpc::Status(::grpc::StatusCode::FAILED_PRECONDITION,
                          "Reading Keyset failed");
  }<commit_msg>Delete a comment I forgot to move in a previous CL -- and also another. Both are basically unnecessary.<commit_after>// Copyright 2020 Google LLC
//
// 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.
//
///////////////////////////////////////////////////////////////////////////////

// Implementation of an AEAD Service.
#include "aead_impl.h"

#include <string>
#include <utility>

#include "tink/aead.h"
#include "tink/binary_keyset_reader.h"
#include "tink/cleartext_keyset_handle.h"

namespace tink_testing_api {

using ::crypto::tink::BinaryKeysetReader;
using ::crypto::tink::CleartextKeysetHandle;

::grpc::Status AeadImpl::CreateAead(grpc::ServerContext* context,
                                    const AeadCreationRequest* request,
                                    AeadCreationResponse* response) {
  auto reader_result = BinaryKeysetReader::New(request->keyset());
  if (!reader_result.ok()) {
    response->set_err(std::string(reader_result.status().message()));
    return ::grpc::Status::OK;
  }
  auto handle_result =
      CleartextKeysetHandle::Read(std::move(reader_result.value()));
  if (!handle_result.ok()) {
    response->set_err(std::string(handle_result.status().message()));
    return ::grpc::Status::OK;
  }
  auto aead_result = handle_result.value()->GetPrimitive<crypto::tink::Aead>();
  if (!aead_result.ok()) {
    response->set_err(std::string(aead_result.status().message()));
    return ::grpc::Status::OK;
  }
  return ::grpc::Status::OK;
}

::grpc::Status AeadImpl::Encrypt(grpc::ServerContext* context,
                                 const AeadEncryptRequest* request,
                                 AeadEncryptResponse* response) {
  auto reader_result = BinaryKeysetReader::New(request->keyset());
  if (!reader_result.ok()) {
    return ::grpc::Status(::grpc::StatusCode::FAILED_PRECONDITION,
                          "Reading Keyset failed");
  }
  auto handle_result =
      CleartextKeysetHandle::Read(std::move(reader_result.value()));
  if (!handle_result.ok()) {
    return ::grpc::Status(::grpc::StatusCode::FAILED_PRECONDITION,
                          "Creating KeysetHandle failed");
  }
  auto aead_result = handle_result.value()->GetPrimitive<crypto::tink::Aead>();
  if (!aead_result.ok()) {
    return ::grpc::Status(::grpc::StatusCode::FAILED_PRECONDITION,
                          "Creating Aead failed");
  }
  auto encrypt_result = aead_result.value()->Encrypt(
      request->plaintext(), request->associated_data());
  if (!encrypt_result.ok()) {
    response->set_err(std::string(encrypt_result.status().message()));
    return ::grpc::Status::OK;
  }
  response->set_ciphertext(encrypt_result.value());
  return ::grpc::Status::OK;
}

::grpc::Status AeadImpl::Decrypt(grpc::ServerContext* context,
                                 const AeadDecryptRequest* request,
                                 AeadDecryptResponse* response) {
  auto reader_result = BinaryKeysetReader::New(request->keyset());
  if (!reader_result.ok()) {
    return ::grpc::Status(::grpc::StatusCode::FAILED_PRECONDITION,
                          "Reading Keyset failed");
  }
{'fraction_non_alphanumeric': 0.15879174172093669, 'fraction_numerical': 0.0022169876680060967, 'mean_word_length': 3.1269296740994856, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '203646', 'n_tokens_mistral': 2032, 'n_tokens_neox': 2059, 'n_words': 441}