summaryrefslogtreecommitdiffstats
path: root/library/cpp/tvmauth/client/misc/roles/decoder.cpp
diff options
context:
space:
mode:
authorkomels <[email protected]>2022-04-15 16:53:39 +0300
committerkomels <[email protected]>2022-04-15 16:53:39 +0300
commit703a2fb6e100d202d1c7fcd052d73bd5affef408 (patch)
tree22b7320c06bb04d86dbf7b9af9ae44281331cd15 /library/cpp/tvmauth/client/misc/roles/decoder.cpp
parent3375bbfda1e2afb03aa2072bf5f2f2c3a26026e8 (diff)
Move 'kikimr/yndx'-depending tests out of ydb/core
ref:0a380e13308d579e0545a76924330d1ca5129c43
Diffstat (limited to 'library/cpp/tvmauth/client/misc/roles/decoder.cpp')
-rw-r--r--library/cpp/tvmauth/client/misc/roles/decoder.cpp93
1 files changed, 0 insertions, 93 deletions
diff --git a/library/cpp/tvmauth/client/misc/roles/decoder.cpp b/library/cpp/tvmauth/client/misc/roles/decoder.cpp
deleted file mode 100644
index 6337fb91c20..00000000000
--- a/library/cpp/tvmauth/client/misc/roles/decoder.cpp
+++ /dev/null
@@ -1,93 +0,0 @@
-#include "decoder.h"
-
-#include <library/cpp/tvmauth/client/misc/utils.h>
-
-#include <library/cpp/openssl/crypto/sha.h>
-#include <library/cpp/streams/brotli/brotli.h>
-#include <library/cpp/streams/zstd/zstd.h>
-
-#include <util/generic/yexception.h>
-#include <util/stream/zlib.h>
-#include <util/string/ascii.h>
-
-namespace NTvmAuth::NRoles {
- TString TDecoder::Decode(const TStringBuf codec, TString&& blob) {
- if (codec.empty()) {
- return std::move(blob);
- }
-
- const TCodecInfo info = ParseCodec(codec);
- TString decoded = DecodeImpl(info.Type, blob);
-
- VerifySize(decoded, info.Size);
- VerifyChecksum(decoded, info.Sha256);
-
- return decoded;
- }
-
- TDecoder::TCodecInfo TDecoder::ParseCodec(TStringBuf codec) {
- const char delim = ':';
-
- const TStringBuf version = codec.NextTok(delim);
- Y_ENSURE(version == "1",
- "unknown codec format version; known: 1; got: " << version);
-
- TCodecInfo res;
- res.Type = codec.NextTok(delim);
- Y_ENSURE(res.Type, "codec type is empty");
-
- const TStringBuf size = codec.NextTok(delim);
- Y_ENSURE(TryIntFromString<10>(size, res.Size),
- "decoded blob size is not number");
-
- res.Sha256 = codec;
- const size_t expectedSha256Size = 2 * NOpenSsl::NSha256::DIGEST_LENGTH;
- Y_ENSURE(res.Sha256.size() == expectedSha256Size,
- "sha256 of decoded blob has invalid length: expected "
- << expectedSha256Size << ", got " << res.Sha256.size());
-
- return res;
- }
-
- TString TDecoder::DecodeImpl(TStringBuf codec, const TString& blob) {
- if (AsciiEqualsIgnoreCase(codec, "brotli")) {
- return DecodeBrolti(blob);
- } else if (AsciiEqualsIgnoreCase(codec, "gzip")) {
- return DecodeGzip(blob);
- } else if (AsciiEqualsIgnoreCase(codec, "zstd")) {
- return DecodeZstd(blob);
- }
-
- ythrow yexception() << "unknown codec: '" << codec << "'";
- }
-
- TString TDecoder::DecodeBrolti(const TString& blob) {
- TStringInput in(blob);
- return TBrotliDecompress(&in).ReadAll();
- }
-
- TString TDecoder::DecodeGzip(const TString& blob) {
- TStringInput in(blob);
- return TZLibDecompress(&in).ReadAll();
- }
-
- TString TDecoder::DecodeZstd(const TString& blob) {
- TStringInput in(blob);
- return TZstdDecompress(&in).ReadAll();
- }
-
- void TDecoder::VerifySize(const TStringBuf decoded, size_t expected) {
- Y_ENSURE(expected == decoded.size(),
- "Decoded blob has bad size: expected " << expected << ", actual " << decoded.size());
- }
-
- void TDecoder::VerifyChecksum(const TStringBuf decoded, const TStringBuf expected) {
- using namespace NOpenSsl::NSha256;
-
- const TDigest dig = Calc(decoded);
- const TString actual = NUtils::ToHex(TStringBuf((char*)dig.data(), dig.size()));
-
- Y_ENSURE(AsciiEqualsIgnoreCase(actual, expected),
- "Decoded blob has bad sha256: expected=" << expected << ", actual=" << actual);
- }
-}