| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- /*
- * Copyright 2009-2017 Alibaba Cloud All rights reserved.
- *
- * 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.
- */
- #pragma once
- #include <memory>
- #include <alibabacloud/oss/Export.h>
- #include <alibabacloud/oss/Types.h>
- namespace AlibabaCloud
- {
- namespace OSS
- {
- enum class CipherAlgorithm {
- AES,
- RSA,
- };
- enum class CipherMode {
- NONE,
- ECB,
- CBC,
- CTR,
- };
- enum class CipherPadding {
- NoPadding,
- PKCS1Padding,
- PKCS5Padding,
- PKCS7Padding,
- ZeroPadding,
- };
- class ALIBABACLOUD_OSS_EXPORT SymmetricCipher
- {
- public:
- virtual ~SymmetricCipher() {};
- //algorithm/mode/padding format. ex. AES/CBC/NoPadding
- const std::string& Name() const { return name_; }
- CipherAlgorithm Algorithm() { return algorithm_; }
- CipherMode Mode() { return mode_; }
- CipherPadding Padding() { return padding_; }
- int BlockSize() { return blockSize_; }
- virtual void EncryptInit(const ByteBuffer& key, const ByteBuffer& iv) = 0;
- virtual ByteBuffer Encrypt(const ByteBuffer& data) = 0;
- virtual int Encrypt(unsigned char * dst, int dstLen, const unsigned char* src, int srcLen) = 0;
- virtual ByteBuffer EncryptFinish() = 0;
- virtual void DecryptInit(const ByteBuffer& key, const ByteBuffer& iv) = 0;
- virtual ByteBuffer Decrypt(const ByteBuffer& data) = 0;
- virtual int Decrypt(unsigned char * dst, int dstLen, const unsigned char* src, int srcLen) = 0;
- virtual ByteBuffer DecryptFinish() = 0;
-
- public:
- static ByteBuffer GenerateIV(size_t length);
- static ByteBuffer GenerateKey(size_t length);
- static ByteBuffer IncCTRCounter(const ByteBuffer& counter, uint64_t numberOfBlocks);
- static std::shared_ptr<SymmetricCipher> CreateAES128_CTRImpl();
- static std::shared_ptr<SymmetricCipher> CreateAES128_CBCImpl();
- static std::shared_ptr<SymmetricCipher> CreateAES256_CTRImpl();
- protected:
- SymmetricCipher(const std::string& impl, CipherAlgorithm algo, CipherMode mode, CipherPadding pad);
- private:
- std::string impl_;
- std::string name_;
- CipherAlgorithm algorithm_;
- CipherMode mode_;
- CipherPadding padding_;
- int blockSize_;
- };
- class ALIBABACLOUD_OSS_EXPORT AsymmetricCipher
- {
- public:
- virtual ~AsymmetricCipher() {};
- const std::string& Name() const { return name_; }
- CipherAlgorithm Algorithm() { return algorithm_; }
- CipherMode Mode() { return mode_; }
- CipherPadding Padding() { return padding_; }
- void setPublicKey(const std::string& key) { publicKey_ = key; }
- void setPrivateKey(const std::string& key) { privateKey_ = key; }
- const std::string& PublicKey() const { return publicKey_; }
- const std::string& PrivateKey() const { return privateKey_; }
- virtual ByteBuffer Encrypt(const ByteBuffer& data) = 0;
- virtual ByteBuffer Decrypt(const ByteBuffer& data) = 0;
- public:
- static std::shared_ptr<AsymmetricCipher> CreateRSA_NONEImpl();
- protected:
- AsymmetricCipher(const std::string& impl, CipherAlgorithm algo, CipherMode mode, CipherPadding pad);
- private:
- std::string impl_;
- std::string name_;
- CipherAlgorithm algorithm_;
- CipherMode mode_;
- CipherPadding padding_;
- std::string publicKey_;
- std::string privateKey_;
- };
- }
- }
|