在C中创建一个二维数组,每个单元格大小为512位。

huangapple go评论97阅读模式
英文:

create 2 dimension array in C, and every cell with 512 bit size

问题

以下是翻译好的内容:

  1. 我想要创建2维数组,维度为2乘以M,数组中的任何单元格都将有512位。我考虑创建类似于以下代码的结构:
  2. #define M 1024
  3. typedef struct {
  4. unsigned char public_key[2][M];
  5. unsigned char private_key[2][M];
  6. } key_pair
  7. void keygen(key *key_pair){
  8. srand(time(NULL))
  9. //
  10. }

但我理解,数组中的每个单元格的大小是256位,因为它是char类型,我希望有512位,我不确定如何解决这个问题的简单方法。

我想在public_key和private_key变量中,每个单元格都有512位的随机值。可能char不是一个好的选择,但我不明白正确的做法是什么,我想我有点困惑。

我在Python中有这段代码,在Python中做这很简单,我也想在C中做到。

  1. def keygen():
  2. SecretKey = np.zeros((2,M), dtype=object)
  3. PublicKey = np.zeros((2,M), dtype=object)
  4. for i in range(0,M):
  5. SecretKey[0][i] = secrets.token_bytes(512/8)
  6. SecretKey[1][i] = secrets.token_bytes(512/8)
  7. PublicKey[0][i] = secrets.token_bytes(512/8)
  8. PublicKey[1][i] = secrets.token_bytes(512/8)
  9. key = [SecretKey, PublicKey]
  10. return key

在Python中,它运行得很好。

英文:

I want to create 2d arrays, with dimension of 2 by M, and any cell in the array will have 512 bit. I was think to create something like this:

  1. #define M 1024
  2. typedef struct {
  3. unsigned char public_key[2][M];
  4. unsigned char private_key[2][M];
  5. } key_pair
  6. void keygen(key *key_pair){
  7. srand(time(NULL))
  8. //
  9. }

But as I can understand, every cell in this array is of size of 256 bit because is type of char, I want 512 bit, I not sure what is the simple idea to solve it.

I want have in public_key and private_key variables, a random value of 512bit in every cell. probably char I not good option, but I not able to understand what is the right way to do it, I think that I'm little a bit confused.

I have the code in Python, in Python its very simple to do it, and i want to do it in C as well.

  1. def keygen():
  2. SecretKey = np.zeros((2,M), dtype=object)
  3. PublicKey = np.zeros((2,M), dtype=object)
  4. for i in range(0,M):
  5. SecretKey[0][i] = secrets.token_bytes(512/8)
  6. SecretKey[1][i] = secrets.token_bytes(512/8)
  7. PublicKey[0][i] = secrets.token_bytes(512/8)
  8. PublicKey[1][i] = secrets.token_bytes(512/8)
  9. key = [SecretKey, PublicKey]
  10. return key

In python its work perfectly

答案1

得分: 1

  1. 创建一个大小为512位的数据类型(例如一个结构体)。

  2. 创建一个数组,其单元格是该数据类型。

  1. #include <limits.h>
  2. #define M 1024
  3. #define CELL_LENGTH_IN_BITS 512
  4. struct cell_s {
  5. unsigned char data[(CELL_LENGTH_IN_BITS + CHAR_BITS - 1) / CHAR_BITS];
  6. };
  7. struct cell_s array[2][M];

在这个示例中,如果CHAR_BITS不是8,单元格的大小可能不是512位,但至少是512位。

英文:
  1. Create a data type (a structure, for example) whose size is 512 bit.
  2. Create an array whose cell is the data type.
  1. #include &lt;limits.h&gt;
  2. #define M 1024
  3. #define CELL_LENGTH_IN_BITS 512
  4. struct cell_s {
  5. unsigned char data[(CELL_LENGTH_IN_BITS + CHAR_BITS - 1) / CHAR_BITS];
  6. };
  7. struct cell_s array[2][M];

In this example, the size of a cell may not be 512 bit if CHAR_BITS is not 8, but at least 512 bit.

huangapple
  • 本文由 发表于 2023年3月7日 21:05:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75662358.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定