/*
* 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. LeetCode447. Number of Boomerangs

    Description Given n points in the plane that are all pairwise distinct, a "boomerang" is a ...

  2. \sum的写法

    \sum默认上下标是写在右上角和右下角的.在独立公式中,则是写在上面和下面的.对于行内公式,我们也可以强制用\limits让其上下表标出现在上面和下面.Note:\sum\nolimits的作用相当于 ...

  3. week 4 ridge regression

    coursera 上的 华盛顿大学 machine learning: regression 第四周笔记 通常, 过拟合的一个表现是拟合模型的参数很大. 为了防止过拟合 Total cost = me ...

  4. DB facade实现CURD

    数据表 CREATE TABLE IF NOT EXISTS students( `id` INT AUTO_INCREMENT PRIMARY KEY, `name` VARCHAR(255) NO ...

  5. TFS2013安装与使用图文教程

    from:http://www.jb51.net/softjc/214560.html TFS2013安装与使用图文教程 一.安装和配置过程介绍 要安装TFS2013当然要先进行下载了,可以在下面的地 ...

  6. MySQL中enum类型数据,要传入字符串

    问题来源:公司业务,某张表中一个字段定义为: enum('0','1','2','3','4','5','6','7','8','9','10') NOT NULL DEFAULT '0' 某天跑脚本 ...

  7. 利用Lucene将被索引文件目录中的所有文件建立索引

    1.新建两个文件夹htm和index,其中htm中存放被索引的文件,index文件中存放建立的索引文件. 2.新建解析目录中所有文件的类,用来解析指定目录下的所有文件. import java.io. ...

  8. python MD5操作

    def my_md5(str): import hashlib new_str = str.encode() #把字符串转成bytes类型 # new_str = b'%s'%str #把字符串转成b ...

  9. SonarQube 平台搭建

    1. 前期准备 ① 环境 jdk 1.8 配置(见其他随笔) MySQL 5.7(见其他随笔) ② 工具下载 sonarqube 下载 <链接:https://pan.baidu.com/s/1 ...

  10. 【BZOJ4898】[Apio2017]商旅 分数规划+SPFA

    [BZOJ4898][Apio2017]商旅 Description 在广阔的澳大利亚内陆地区长途跋涉后,你孤身一人带着一个背包来到了科巴.你被这个城市发达而美丽的市场所深深吸引,决定定居于此,做一个 ...