/*
* sequence.c
*/
#include <stdio.h>
#include <memory.h> /*
* SM3算法产生的哈希值大小(单位:字节)
*/
#define SM3_HASH_SIZE 32 /*
* SM3上下文
*/
typedef struct SM3Context
{
unsigned int intermediateHash[SM3_HASH_SIZE / ];
unsigned char messageBlock[];
} SM3Context; /*
* 判断运行环境是否为小端
*/
static const int endianTest = ;
#define IsLittleEndian() (*(char *)&endianTest == 1) /*
* 向左循环移位
*/
#define LeftRotate(word, bits) ( (word) << (bits) | (word) >> (32 - (bits)) ) /*
* 反转四字节整型字节序
*/
unsigned int *ReverseWord(unsigned int *word)
{
unsigned char *byte, temp; byte = (unsigned char *)word;
temp = byte[];
byte[] = byte[];
byte[] = temp; temp = byte[];
byte[] = byte[];
byte[] = temp;
return word;
} /*
* T
*/
unsigned int T(int i)
{
if (i >= && i <= )
return 0x79CC4519;
else if (i >= && i <= )
return 0x7A879D8A;
else
return ;
} /*
* FF
*/
unsigned int FF(unsigned int X, unsigned int Y, unsigned int Z, int i)
{
if (i >= && i <= )
return X ^ Y ^ Z;
else if (i >= && i <= )
return (X & Y) | (X & Z) | (Y & Z);
else
return ;
} /*
* GG
*/
unsigned int GG(unsigned int X, unsigned int Y, unsigned int Z, int i)
{
if (i >= && i <= )
return X ^ Y ^ Z;
else if (i >= && i <= )
return (X & Y) | (~X & Z);
else
return ;
} /*
* P0
*/
unsigned int P0(unsigned int X)
{
return X ^ LeftRotate(X, ) ^ LeftRotate(X, );
} /*
* P1
*/
unsigned int P1(unsigned int X)
{
return X ^ LeftRotate(X, ) ^ LeftRotate(X, );
} /*
* 初始化函数
*/
void SM3Init(SM3Context *context)
{
context->intermediateHash[] = 0x7380166F;
context->intermediateHash[] = 0x4914B2B9;
context->intermediateHash[] = 0x172442D7;
context->intermediateHash[] = 0xDA8A0600;
context->intermediateHash[] = 0xA96F30BC;
context->intermediateHash[] = 0x163138AA;
context->intermediateHash[] = 0xE38DEE4D;
context->intermediateHash[] = 0xB0FB0E4E;
} /*
* 处理消息块
*/
void SM3ProcessMessageBlock(SM3Context *context)
{
int i;
unsigned int W[];
unsigned int W_[];
unsigned int A, B, C, D, E, F, G, H, SS1, SS2, TT1, TT2; /* 消息扩展 */
for (i = ; i < ; i++)
{
W[i] = *(unsigned int *)(context->messageBlock + i * );
if (IsLittleEndian())
ReverseWord(W + i);
//printf("%d: %x\n", i, W[i]);
}
for (i = ; i < ; i++)
{
W[i] = P1(W[i - ] ^ W[i - ] ^ LeftRotate(W[i - ], ))
^ LeftRotate(W[i - ], )
^ W[i - ];
//printf("%d: %x\n", i, W[i]);
}
for (i = ; i < ; i++)
{
W_[i] = W[i] ^ W[i + ];
//printf("%d: %x\n", i, W_[i]);
} /* 消息压缩 */
A = context->intermediateHash[];
B = context->intermediateHash[];
C = context->intermediateHash[];
D = context->intermediateHash[];
E = context->intermediateHash[];
F = context->intermediateHash[];
G = context->intermediateHash[];
H = context->intermediateHash[];
for (i = ; i < ; i++)
{
SS1 = LeftRotate((LeftRotate(A, ) + E + LeftRotate(T(i), i)), );
SS2 = SS1 ^ LeftRotate(A, );
TT1 = FF(A, B, C, i) + D + SS2 + W_[i];
TT2 = GG(E, F, G, i) + H + SS1 + W[i];
D = C;
C = LeftRotate(B, );
B = A;
A = TT1;
H = G;
G = LeftRotate(F, );
F = E;
E = P0(TT2);
}
context->intermediateHash[] ^= A;
context->intermediateHash[] ^= B;
context->intermediateHash[] ^= C;
context->intermediateHash[] ^= D;
context->intermediateHash[] ^= E;
context->intermediateHash[] ^= F;
context->intermediateHash[] ^= G;
context->intermediateHash[] ^= H;
} /*
* SM3算法主函数
*/
unsigned char *SM3Calc(const unsigned char *message,
unsigned int messageLen, unsigned char digest[SM3_HASH_SIZE])
{
SM3Context context;
unsigned int i, remainder, bitLen; SM3Init(&context); for (i = ; i < messageLen / ; i++)
{
memcpy(context.messageBlock, message + i * , );
SM3ProcessMessageBlock(&context);
} bitLen = messageLen * ;
if (IsLittleEndian())
ReverseWord(&bitLen);
remainder = messageLen % ;
memcpy(context.messageBlock, message + i * , remainder);
context.messageBlock[remainder] = 0x80;
if (remainder <= )
{
memset(context.messageBlock + remainder + , , - remainder - - + );
memcpy(context.messageBlock + - , &bitLen, );
SM3ProcessMessageBlock(&context);
}
else
{
memset(context.messageBlock + remainder + , , - remainder - );
SM3ProcessMessageBlock(&context);
memset(context.messageBlock, , - );
memcpy(context.messageBlock + - , &bitLen, );
SM3ProcessMessageBlock(&context);
} if (IsLittleEndian())
for (i = ; i < ; i++)
ReverseWord(context.intermediateHash + i);
memcpy(digest, context.intermediateHash, SM3_HASH_SIZE); return digest;
} /*
* 计算SM3,并将结果以无符号整数形式返回
*/
unsigned int SM3CalcAndReturnUInt(const unsigned char *message, unsigned int messageLen)
{
int i;
unsigned int s;
unsigned char digest[SM3_HASH_SIZE]; SM3Calc(message, messageLen, digest);
for (i = , s = ; i < SM3_HASH_SIZE; i += )
{
s += digest[i + ] << |
digest[i + ] << |
digest[i + ] << |
digest[i + ] << ;
} return s;
} int main(void)
{
printf("%x\n", SM3CalcAndReturnUInt("\x1\x2\x3\x4\x5\x6", strlen("\x1\x2\x3\x4\x5\x6")));
return ;
}

sequence.c的更多相关文章

  1. oracle SEQUENCE 创建, 修改,删除

    oracle创建序列化: CREATE SEQUENCE seq_itv_collection            INCREMENT BY 1  -- 每次加几个              STA ...

  2. Oracle数据库自动备份SQL文本:Procedure存储过程,View视图,Function函数,Trigger触发器,Sequence序列号等

    功能:备份存储过程,视图,函数触发器,Sequence序列号等准备工作:--1.创建文件夹 :'E:/OracleBackUp/ProcBack';--文本存放的路径--2.执行:create or ...

  3. DG gap sequence修复一例

    环境:Oracle 11.2.0.4 DG 故障现象: 客户在备库告警日志中发现GAP sequence提示信息: Mon Nov 21 09:53:29 2016 Media Recovery Wa ...

  4. Permutation Sequence

    The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  5. [LeetCode] Sequence Reconstruction 序列重建

    Check whether the original sequence org can be uniquely reconstructed from the sequences in seqs. Th ...

  6. [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列

    Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...

  7. [LeetCode] Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列

    Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...

  8. [LeetCode] Longest Consecutive Sequence 求最长连续序列

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  9. [LeetCode] Permutation Sequence 序列排序

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  10. Leetcode 60. Permutation Sequence

    The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

随机推荐

  1. Idea 2017的激活方式

    https://blog.csdn.net/wangyuanjun008/article/details/79233491

  2. shell实现倒计时功能

    #!/bin/bash ############################################################## # File Name: oldboyedu.sh ...

  3. JDBC mysql 中文乱码

    中文乱码似乎是程序编写中永恒的一个话题和难点,就比如MySQL存取中文乱码,但我想做任何事情,都要有个思路才行,有了思路才知道如何去解决问题,否则,即使一时解决了问题,但过后不久又碰到同样的问题可能又 ...

  4. CMake 简介与使用

    cross platform make的缩写. 是一个比make更高级的编译配置工具,它可以根据不同平台.不同的编译器,生成相应的Makefile或者vcproj项目文件.通过编写CMakeLists ...

  5. js 把字符串当做方法执行

    <SCRIPT LANGUAGE="JavaScript"> function test(str){ alert(str); } eval('test("aa ...

  6. UML Rose2003完美破解攻略

    Rational Rose 2003 软件project画图软件 ,当然还不止画图,对于那些不想用英文版Rational Rose2003的同志们.这个Rational Rose2003 版本号已经汉 ...

  7. 数值孔径NA、分辨率极限与衍射极限

    一.数值孔径  数值孔径(NA):是一个无量纲的数,用以衡量该系统能够收集的光的角度范围.越大,收集到的光越多,分辨率越高. 描述了透镜收光锥角的大小,决定着透镜收光能力和空间分辨率. 数值孔径(NA ...

  8. python3----strip lstrip rstrip

    Python中的strip用于去除字符串的首位字符,同理,lstrip用于去除左边的字符,rstrip用于去除右边的字符.这三个函数都可传入一个参数,指定要去除的首尾字符.注意的是,传入的是一个字符数 ...

  9. 《PhotoShop CS6 》第一节 矢量与分辨率

    分辨率:不一定是方形,可以调整其比例. 色彩模型:色相Hue(圆周,冷暖相接),饱和度Saturation(半径),明度Brightness(轴,从黑到白).

  10. Youth Is Not a Time of Life

    Youth is not a time of life; it is a state of mind.青春不是年华,而是心境: It is not a matter of rosy cheeks, r ...