Converting Compressed or Uncompressed ECC Point Data (C/C++)
You can convert compressed or uncompressed point data into a Point object for generating a key object, or convert a Point object into compressed or uncompressed point data.
For details about the ECC algorithm specifications, see ECC.
You can pass in the string parameter format to specify the format of the point data to obtain. To obtain compressed point data, set format to COMPRESSED. To obtain uncompressed point data, set format to UNCOMPRESSED.
Converting Uncompressed Point Data into Compressed Point Data
- Pass the uncompressed ECC point data of the uint8_t type, and call OH_CryptoEcPoint_Create to construct a OH_CryptoEcPoint object for generating point data.
- Call OH_CryptoEcPoint_Encode to obtain the compressed point data.
#include "CryptoArchitectureKit/crypto_architecture_kit.h"
static OH_Crypto_ErrCode doTestEccPointUncompressedToCompressed()
{
uint8_t pk[] = {
4, 143, 39, 57, 249, 145, 50, 63, 222, 35, 70, 178, 121, 202, 154, 21, 146, 129, 75, 76, 63, 8, 195, 157, 111,
40, 217, 215, 148, 120, 224, 205, 82, 83, 92, 185, 21, 211, 184, 5, 19, 114, 33, 86, 85, 228, 123, 242, 206,
200, 98, 178, 184, 130, 35, 232, 45, 5, 202, 189, 11, 46, 163, 156, 152
};
Crypto_DataBlob pkData = {pk, sizeof(pk)};
OH_CryptoEcPoint *point = nullptr;
OH_Crypto_ErrCode ret = OH_CryptoEcPoint_Create("NID_brainpoolP256r1", &pkData, &point);
if (ret != CRYPTO_SUCCESS) {
return ret;
}
Crypto_DataBlob returnPointBlobData = {0};
ret = OH_CryptoEcPoint_Encode(point, "COMPRESSED", &returnPointBlobData);
if (ret != CRYPTO_SUCCESS) {
OH_CryptoEcPoint_Destroy(point);
return ret;
}
OH_Crypto_FreeDataBlob(&returnPointBlobData);
OH_CryptoEcPoint_Destroy(point);
return ret;
}
Converting Compressed Point Data into a Key Object
- Pass the compressed ECC point data of the uint8_t type, and call OH_CryptoEcPoint_Create to construct a OH_CryptoEcPoint object for generating point data.
- Call OH_CryptoEcPoint_Encode to obtain the uncompressed point data.
#include "CryptoArchitectureKit/crypto_architecture_kit.h"
static OH_Crypto_ErrCode doTestEccPointCompressedToPoint()
{
uint8_t pk[] = {
2, 143, 39, 57, 249, 145, 50, 63, 222, 35, 70, 178, 121, 202, 154, 21, 146, 129, 75, 76, 63, 8, 195, 157, 111,
40, 217, 215, 148, 120, 224, 205, 82
};
Crypto_DataBlob pkData = {pk, sizeof(pk)};
OH_CryptoEcPoint *point = nullptr;
OH_Crypto_ErrCode ret = OH_CryptoEcPoint_Create("NID_brainpoolP256r1", &pkData, &point);
if (ret != CRYPTO_SUCCESS) {
return ret;
}
Crypto_DataBlob returnPointBlobData = {0};
ret = OH_CryptoEcPoint_Encode(point, "UNCOMPRESSED", &returnPointBlobData);
if (ret != CRYPTO_SUCCESS) {
OH_CryptoEcPoint_Destroy(point);
return ret;
}
OH_Crypto_FreeDataBlob(&returnPointBlobData);
OH_CryptoEcPoint_Destroy(point);
return ret;
}
你可能感兴趣的鸿蒙文章
openharmony 鸿蒙 crypto-rsa-asym-encrypt-decrypt-pkcs1-ndk
openharmony 鸿蒙 crypto-key-agreement-using-ecdh
openharmony 鸿蒙 crypto-aes-sym-encrypt-decrypt-gcm-ndk
openharmony 鸿蒙 crypto-sm4-sym-encrypt-decrypt-gcm-ndk
openharmony 鸿蒙 crypto-sm2-sign-sig-verify-pkcs1-ndk
openharmony 鸿蒙 crypto-key-derivation-using-scrypt-ndk
openharmony 鸿蒙 crypto-sym-encrypt-decrypt-spec
openharmony 鸿蒙 crypto-rsa-sign-sig-verify-pkcs1-by-segment