with or without you

更新时间:2022-11-24 23:26:38 阅读: 评论:0


2022年11月24日发(作者:爱情公寓3主题曲叫什么)

hmacsha256c语⾔源码,纯C语⾔实现hmacsha256,可在单⽚

机中使⽤_沃航科技

这是⼩沃最近遇到的⼀个项⽬,由于百度云的所有接⼝都需要通过hmacsha256计算,所以⼩沃在⽹上找到了相关代码可以在单⽚机中实

现,现在就分享给⼤家。

SHA256.h#ifndefSHA256_H

#defineSHA256_H

#include

typedefstruct{

uint8_thash[32];

uint32_tbuffer[16];

uint32_tstate[8];

uint8_tlength[8];

}sha256;

externvoidsha256_get(uint8_thash[32],

constuint8_t*message,

intlength);

externvoidhmac_sha256_get(uint8_tdigest[32],

uint8_t*message,intmessage_length,

uint8_t*key,intkey_length);

#endif

SHA256.c/*

*Copyright2006AppleComputer,htsrerved.

*

*iTunesUSampleCodeLicen

*IMPORTANT:ThisApplesoftwareissuppliedtoyoubyAppleComputer,Inc.("Apple")

*inconsiderationofyouragreementtothefollowingterms,andyouru,

*installation,modificationordistributionofthisApplesoftwareconstitutes

*onotagreewiththeterms,pleadonotu,

*install,modifyordistributethisApplesoftware.

*

*Inconsiderationofyouragreementtoabidebythefollowingtermsandsubjectto

*theterms,Applegrantsyouapersonal,non-exclusive,non-transferablelicen,

*underApple'scopyrightsinthisoriginalApplesoftware(the"AppleSoftware"):

*

*(a)tointernallyu,reproduce,modifyandinternallydistributetheApple

*Software,withorwithoutmodifications,insourceandbinaryforms,withinyour

*educationalorganizationorinternalcampusnetworkforthesolepurpoof

*integratingApple'siTunesUsoftwarewithyourinternalcampusnetworksystems;and

*

*(b)toredistributetheAppleSoftwaretootheruniversitiesoreducational

*organizations,withorwithoutmodifications,insourceandbinaryforms,forthe

*solepurpoofintegratingApple'siTunesUsoftwarewiththeirinternalcampus

*networksystems;providedthatthefollowingconditionsaremet:

*

*-IfyouredistributetheAppleSoftwareinitntiretyandwithout

*modifications,youmustretaintheabovecopyrightnotice,thintirelicen

*andthedisclaimerprovisionsinallsuchredistributionsoftheAppleSoftware.

*-IfyoumodifyandredistributetheAppleSoftware,youmustindicatethatyou

*havemadechangestotheAppleSoftware,andyoumustretaintheabove

*copyrightnotice,thintirelicenandthedisclaimerprovisionsinall

*suchredistributionsoftheAppleSoftwareand/orderivativesthereofcreated

*byyou.

*-Neitherthename,trademarks,rvicemarksorlogosofApplemaybeudto

*endororpromoteproductsderivedfromtheAppleSoftwarewithoutspecific

*priorwrittenpermissionfromApple.

*

*Exceptaxpresslystatedabove,nootherrightsorlicens,expressorimplied,

*aregrantedbyAppleherein,includingbutnotlimitedtoanypatentrightsthatmay

*beinfringedbyyourderivativeworksorbyotherworksinwhichtheAppleSoftware

*LESOFTWAREISPROVIDEDBYAPPLEONAN"ASIS"BASIS.

*APPLEMAKESNOWARRANTIES,EXPRESSORIMPLIED,ANDHEREBYDISCLAIMSALLWARRANTIES,

*INCLUDINGWITHOUTLIMITATIONTHEIMPLIEDWARRANTIESOFNON-INFRINGEMENT,

*MERCHANTABILITYANDFITNESSFORAPARTICULARPURPOSE,REGARDINGTHEAPPLESOFTWARE

*ORITSUSEANDOPERATIONALONEORINCOMBINATIONWITHYOURPRODUCTSORSYSTEMS.

*APPLEISNOTOBLIGATEDTOPROVIDEANYMAINTENANCE,TECHNICALOROTHERSUPPORTFOR

*THEAPPLESOFTWARE,ENT

*SHALLAPPLEBELIABLEFORANYDIRECT,SPECIAL,INDIRECT,INCIDENTALOR

*CONSEQUENTIALDAMAGES(INCLUDING,BUTNOTLIMITEDTO,PROCUREMENTOFSUBSTITUTE

*GOODSORSERVICES;LOSSOFUSE,DATA,ORPROFITS;ORBUSINESSINTERRUPTION)

*ARISINGINANYWAYOUTOFTHEUSE,REPRODUCTION,MODIFICATIONAND/ORDISTRIBUTION

*OFTHEAPPLESOFTWARE,HOWEVERCAUSEDANDWHETHERUNDERTHEORYOFCONTRACT,TORT

*(INCLUDINGNEGLIGENCE),STRICTLIABILITYOROTHERWISE,EVENIFAPPLEHASBEEN

*ADVISEDOFTHEPOSSIBILITYOFSUCHDAMAGE.

*

*Rev.120806

*

*Thissourcecodefilecontainsalf-containedANSICprogramwithno

*SX,it

*canbecompiledandrunbyexecutingthefollowingcommandsinaterminal

*window:

*gcc-ocondsconds.c

*./conds

*/

//CompilenoteaddedbyRKW

//gcc-ohmac-sha256hmac-sha256.c

//shouldworkonlatter-daygccinstalls,butc99canbemadeexplicitthisway:

//gcc-std=c99-ohmac-sha256hmac-sha256.c

#include

#include

#include

/*#include*/

#include//AddedbyRKW,neededfortypesuint8_t,uint32_t;requiresC99compiler

#include"SHA256.h"

/******************************************************************************

*SHA-256.

*/

voidsha256_initialize(sha256*sha){

inti;

for(i=0;ibuffer[i]=0;

sha->state[0]=0x6a09e667;

sha->state[1]=0xbb67ae85;

sha->state[2]=0x3c6ef372;

sha->state[3]=0xa54ff53a;

sha->state[4]=0x510e527f;

sha->state[5]=0x9b05688c;

sha->state[6]=0x1f83d9ab;

sha->state[7]=0x5be0cd19;

for(i=0;ilength[i]=0;

}

//ChangedbyRKW,formalargsarenowconstuint8_t,uint_32

//fromconstunsignedchar,unsignedlongrespectively

voidsha256_update(sha256*sha,

constuint8_t*message,

uint32_tlength){

inti,j;

/*Addthelengthofthereceivedmessage,countedin

*bytes,tothetotallengthofthemessageshashedto

*date,countedinbitsandstoredin8paratebytes.*/

for(i=7;i>=0;--i){

intbits;

if(i==7)

bits=length<

elif(i==0||i==1||i==2)

bits=0;

el

bits=length>>(53-8*i);

bits&=0xff;

if(sha->length[i]+bits>0xff){

for(j=i-1;j>=0&&sha->length[j]++==0xff;--j);

}

sha->length[i]+=bits;

}

/*AddthereceivedmessagetotheSHAbuffer,updatingthe

*hashateachblock(eachtimethebufferisfilled).*/

while(length>0){

/*FindtheindexintheSHAbufferatwhichto

*appendwhat'sleftofthereceivedmessage.*/

intindex=sha->length[6]%2*32+sha->length[7]/8;

index=(index+64-length%64)%64;

/*AppendthereceivedmessagebytestotheSHAbufferuntil

*werunoutofmessagebytesoruntilthebufferisfilled.*/

for(;length>0&&index

sha->buffer[index/4]|=*message<

}

/*Updatethehashwiththebuffercontentsifthebufferisfull.*/

if(index==64){

/*S180-2

*()

*foradescriptionofanddetailsonthealgorithmudhere.*/

//ChangedbyRKW,constunsignedlongbecomesconstuint32_t

constuint32_tk[64]={

0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5,

0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5,

0xd807aa98,0x12835b01,0x243185be,0x550c7dc3,

0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174,

0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc,

0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da,

0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7,

0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967,

0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13,

0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85,

0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3,

0xd192e819,0xd6990624,0xf40e3585,0x106aa070,

0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5,

0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3,

0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208,

0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2

};

//ChangedbyRKW,unsignedlongbecomesuint32_t

uint32_tw[64],a,b,c,d,e,f,g,h;

intt;

for(t=0;t

w[t]=sha->buffer[t];

sha->buffer[t]=0;

}

for(t=16;t

//ChangedbyRKW,unsignedlongbecomesuint32_t

uint32_ts0,s1;

s0=(w[t-15]>>7|w[t-15]<

s0^=(w[t-15]>>18|w[t-15]<

s0^=(w[t-15]>>3);

s1=(w[t-2]>>17|w[t-2]<

s1^=(w[t-2]>>19|w[t-2]<

s1^=(w[t-2]>>10);

w[t]=(s1+w[t-7]+s0+w[t-16])&0xffffffffU;

}

a=sha->state[0];

b=sha->state[1];

c=sha->state[2];

d=sha->state[3];

e=sha->state[4];

f=sha->state[5];

g=sha->state[6];

h=sha->state[7];

for(t=0;t

//ChangedbyRKW,unsignedlongbecomesuint32_t

uint32_te0,e1,t1,t2;

e0=(a>>2|a<

e0^=(a>>13|a<

e0^=(a>>22|a<

e1=(e>>6|e<

e1^=(e>>11|e<

e1^=(e>>25|e<

t1=h+e1+((e&f)^(~e&g))+k[t]+w[t];

t2=e0+((a&b)^(a&c)^(b&c));

h=g;

g=f;

f=e;

e=d+t1;

d=c;

c=b;

b=a;

a=t1+t2;

}

sha->state[0]=(sha->state[0]+a)&0xffffffffU;

sha->state[1]=(sha->state[1]+b)&0xffffffffU;

sha->state[2]=(sha->state[2]+c)&0xffffffffU;

sha->state[3]=(sha->state[3]+d)&0xffffffffU;

sha->state[4]=(sha->state[4]+e)&0xffffffffU;

sha->state[5]=(sha->state[5]+f)&0xffffffffU;

sha->state[6]=(sha->state[6]+g)&0xffffffffU;

sha->state[7]=(sha->state[7]+h)&0xffffffffU;

}

}

}

//ChangedbyRKW,formalargsarenowconstuint8_t,uint_32

//fromconstunsignedchar,unsignedlongrespectively

voidsha256_finalize(sha256*sha,

constuint8_t*message,

uint32_tlength){

inti;

//ChangedbyRKW,unsignedcharbecomesuint8_t

uint8_tterminator[64+8]={0x80};

/*Hashthefinalmessagebytesifnecessary.*/

if(length>0)sha256_update(sha,message,length);

/*Createaterminatorthatincludesastopbit,padding,and

*S180-2fordetails.*/

length=64-sha->length[6]%2*32-sha->length[7]/8;

if(length

for(i=0;ilength[i];

/*Hashtheterminatortofinalizethemessagedigest.*/

sha256_update(sha,terminator,length);

/*Extractthemessagedigest.*/

for(i=0;i

sha->hash[i]=(sha->state[i/4]>>(24-8*(i%4)))&0xff;

}

}

//ChangedbyRKW,formalargsarenowuint8_t,constuint_8

//fromunsignedchar,constunsignedcharrespectively

voidsha256_get(uint8_thash[32],

constuint8_t*message,

intlength){

inti;

sha256sha;

sha256_initialize(&sha);

sha256_finalize(&sha,message,length);

for(i=0;i

}

/******************************************************************************

*HMAC-SHA256.

*/

typedefstruct_hmac_sha256{

uint8_tdigest[32];//ChangedbyRKW,unsignedcharbecomesuint_8

uint8_tkey[64];//ChangedbyRKW,unsignedcharbecomesuint_8

sha256sha;

}hmac_sha256;

//ChangedbyRKW,formalargisnowconstuint8_t

//fromconstunsignedchar

voidhmac_sha256_initialize(hmac_sha256*hmac,

constuint8_t*key,intlength){

inti;

/*Preparetheinnerhashkeyblock,hashingthekeyifit'stoolong.*/

if(length<=64){

for(i=0;ikey[i]=key[i]^0x36;

for(;ikey[i]=0x36;

}el{

sha256_initialize(&(hmac->sha));

sha256_finalize(&(hmac->sha),key,length);

for(i=0;ikey[i]=hmac->[i]^0x36;

for(;ikey[i]=0x36;

}

/*Initializetheinnerhashwiththekeyblock.*/

sha256_initialize(&(hmac->sha));

sha256_update(&(hmac->sha),hmac->key,64);

}

//ChangedbyRKW,formalargisnowconstuint8_t

//fromconstunsignedchar

voidhmac_sha256_update(hmac_sha256*hmac,

constuint8_t*message,intlength){

/*Updatetheinnerhash.*/

sha256_update(&(hmac->sha),message,length);

}

//ChangedbyRKW,formalargisnowconstuint8_t

//fromconstunsignedchar

voidhmac_sha256_finalize(hmac_sha256*hmac,

constuint8_t*message,intlength){

inti;

/*Finalizetheinnerhashandstoreitsvalueinthedigestarray.*/

sha256_finalize(&(hmac->sha),message,length);

for(i=0;idigest[i]=hmac->[i];

/*Converttheinnerhashkeyblocktotheouterhashkeyblock.*/

for(i=0;ikey[i]^=(0x36^0x5c);

/*Calculatetheouterhash.*/

sha256_initialize(&(hmac->sha));

sha256_update(&(hmac->sha),hmac->key,64);

sha256_finalize(&(hmac->sha),hmac->digest,32);

/*UtheouterhashvalueastheHMACdigest.*/

for(i=0;idigest[i]=hmac->[i];

}

//ChangedbyRKW,formalargsarenowuint8_t,constuint8_t

//fromunsingedchar,constunsignedcharrespectively

voidhmac_sha256_get(uint8_tdigest[32],

uint8_t*message,intmessage_length,

uint8_t*key,intkey_length){

inti;

hmac_sha256hmac;

hmac_sha256_initialize(&hmac,key,key_length);

hmac_sha256_finalize(&hmac,message,message_length);

for(i=0;i

}

voidsha256_get(uint8_thash[32],constuint8_t*message,intlength);//此函数⽤于对消息计算摘要值,输⼊任意⼤⼩消息,输

出32字节摘要值

voidhmac_sha256_get(uint8_tdigest[32],uint8_t*message,intmessage_length,uint8_t*key,intkey_length);//此函数⽤于

HMAC_SHA256加密,秘钥任意长度,输出32字节

需要⾃⼰转成对应16进字字符串哦。

⽂章作者:沃航科技

本文发布于:2022-11-24 23:26:38,感谢您对本站的认可!

本文链接:http://www.wtabcd.cn/fanwen/fan/90/14908.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

上一篇:宋美龄演讲
下一篇:the wizard of oz
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图