blob: 89029d0fd68abcafd137ff1d546881bec8ee9682 [file]
// Copyright 2026 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
//
// https://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.
//
#include "crypto/algorithms.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
namespace credentio {
absl::StatusOr<HashAlgorithm> NidToHashAlgorithm(int nid) {
HashAlgorithm algorithm = static_cast<HashAlgorithm>(nid);
switch (algorithm) {
case HashAlgorithm::kSha256:
case HashAlgorithm::kSha384:
case HashAlgorithm::kSha512:
return algorithm;
}
return absl::InvalidArgumentError(
absl::StrCat("hash algorithm NID is not on C2PA allowlist: ", nid));
}
absl::StatusOr<HashAlgorithm> ParseHashAlgorithm(absl::string_view txt) {
if (txt == "sha256") {
return HashAlgorithm::kSha256;
} else if (txt == "sha384") {
return HashAlgorithm::kSha384;
} else if (txt == "sha512") {
return HashAlgorithm::kSha512;
} else {
return absl::InvalidArgumentError("unrecognized hash algorithm name");
}
}
} // namespace credentio