使用hmac和sha256进行Python加密并不能提供与javascript相同的结果

时间:2017-12-12 00:59:36

标签: javascript python json encryption

我试图在python和javascript中获得相同的结果但是当加密时我遇到了麻烦,加密在javascript和python中给了我相同的结果但是如果我把数据放在json格式然后加密我得到不同的结果。我可以更改javascript代码,因为对于api,如果需要可以修改python代码。
Python代码:

from datetime import datetime
import json
import hashlib
import hmac
import base64

s="2017-12-11 23:31:55"
data1=dict(timestamp=s)
msg_data = json.dumps(data1)

print msg_data

message = bytes(s).encode('utf-8')
secret = bytes("secret").encode('utf-8')
msg_data2 = bytes(msg_data).encode('utf-8')

signature = base64.b64encode(hmac.new(secret, message, digestmod=hashlib.sha256).digest())
print "without json"
print(signature)    #XZqVjDKYG8NRITjiMNXFZnNdUDb+JIr7Vm+f3CabETY=

signature = base64.b64encode(hmac.new(secret, msg_data2, digestmod=hashlib.sha256).digest())
print "with json"
print(signature)    #RnxbwptM7HuEE1SPResxc4A6tVDqhv3DxWP75g8pfik=

JS代码:

<script type="text/javascript">
    /*
     https://raw.githubusercontent.com/jkiss/crypto-js/master/tags/3.1.2/build/rollups/hmac-sha256.js
CryptoJS v3.1.2
code.google.com/p/crypto-js
(c) 2009-2013 by Jeff Mott. All rights reserved.
code.google.com/p/crypto-js/wiki/License
*/
var CryptoJS=CryptoJS||function(h,s){var f={},g=f.lib={},q=function(){},m=g.Base={extend:function(a){q.prototype=this;var c=new q;a&&c.mixIn(a);c.hasOwnProperty("init")||(c.init=function(){c.$super.init.apply(this,arguments)});c.init.prototype=c;c.$super=this;return c},create:function(){var a=this.extend();a.init.apply(a,arguments);return a},init:function(){},mixIn:function(a){for(var c in a)a.hasOwnProperty(c)&&(this[c]=a[c]);a.hasOwnProperty("toString")&&(this.toString=a.toString)},clone:function(){return this.init.prototype.extend(this)}},
r=g.WordArray=m.extend({init:function(a,c){a=this.words=a||[];this.sigBytes=c!=s?c:4*a.length},toString:function(a){return(a||k).stringify(this)},concat:function(a){var c=this.words,d=a.words,b=this.sigBytes;a=a.sigBytes;this.clamp();if(b%4)for(var e=0;e<a;e++)c[b+e>>>2]|=(d[e>>>2]>>>24-8*(e%4)&255)<<24-8*((b+e)%4);else if(65535<d.length)for(e=0;e<a;e+=4)c[b+e>>>2]=d[e>>>2];else c.push.apply(c,d);this.sigBytes+=a;return this},clamp:function(){var a=this.words,c=this.sigBytes;a[c>>>2]&=4294967295<<
32-8*(c%4);a.length=h.ceil(c/4)},clone:function(){var a=m.clone.call(this);a.words=this.words.slice(0);return a},random:function(a){for(var c=[],d=0;d<a;d+=4)c.push(4294967296*h.random()|0);return new r.init(c,a)}}),l=f.enc={},k=l.Hex={stringify:function(a){var c=a.words;a=a.sigBytes;for(var d=[],b=0;b<a;b++){var e=c[b>>>2]>>>24-8*(b%4)&255;d.push((e>>>4).toString(16));d.push((e&15).toString(16))}return d.join("")},parse:function(a){for(var c=a.length,d=[],b=0;b<c;b+=2)d[b>>>3]|=parseInt(a.substr(b,
2),16)<<24-4*(b%8);return new r.init(d,c/2)}},n=l.Latin1={stringify:function(a){var c=a.words;a=a.sigBytes;for(var d=[],b=0;b<a;b++)d.push(String.fromCharCode(c[b>>>2]>>>24-8*(b%4)&255));return d.join("")},parse:function(a){for(var c=a.length,d=[],b=0;b<c;b++)d[b>>>2]|=(a.charCodeAt(b)&255)<<24-8*(b%4);return new r.init(d,c)}},j=l.Utf8={stringify:function(a){try{return decodeURIComponent(escape(n.stringify(a)))}catch(c){throw Error("Malformed UTF-8 data");}},parse:function(a){return n.parse(unescape(encodeURIComponent(a)))}},
u=g.BufferedBlockAlgorithm=m.extend({reset:function(){this._data=new r.init;this._nDataBytes=0},_append:function(a){"string"==typeof a&&(a=j.parse(a));this._data.concat(a);this._nDataBytes+=a.sigBytes},_process:function(a){var c=this._data,d=c.words,b=c.sigBytes,e=this.blockSize,f=b/(4*e),f=a?h.ceil(f):h.max((f|0)-this._minBufferSize,0);a=f*e;b=h.min(4*a,b);if(a){for(var g=0;g<a;g+=e)this._doProcessBlock(d,g);g=d.splice(0,a);c.sigBytes-=b}return new r.init(g,b)},clone:function(){var a=m.clone.call(this);
a._data=this._data.clone();return a},_minBufferSize:0});g.Hasher=u.extend({cfg:m.extend(),init:function(a){this.cfg=this.cfg.extend(a);this.reset()},reset:function(){u.reset.call(this);this._doReset()},update:function(a){this._append(a);this._process();return this},finalize:function(a){a&&this._append(a);return this._doFinalize()},blockSize:16,_createHelper:function(a){return function(c,d){return(new a.init(d)).finalize(c)}},_createHmacHelper:function(a){return function(c,d){return(new t.HMAC.init(a,
d)).finalize(c)}}});var t=f.algo={};return f}(Math);
(function(h){for(var s=CryptoJS,f=s.lib,g=f.WordArray,q=f.Hasher,f=s.algo,m=[],r=[],l=function(a){return 4294967296*(a-(a|0))|0},k=2,n=0;64>n;){var j;a:{j=k;for(var u=h.sqrt(j),t=2;t<=u;t++)if(!(j%t)){j=!1;break a}j=!0}j&&(8>n&&(m[n]=l(h.pow(k,0.5))),r[n]=l(h.pow(k,1/3)),n++);k++}var a=[],f=f.SHA256=q.extend({_doReset:function(){this._hash=new g.init(m.slice(0))},_doProcessBlock:function(c,d){for(var b=this._hash.words,e=b[0],f=b[1],g=b[2],j=b[3],h=b[4],m=b[5],n=b[6],q=b[7],p=0;64>p;p++){if(16>p)a[p]=
c[d+p]|0;else{var k=a[p-15],l=a[p-2];a[p]=((k<<25|k>>>7)^(k<<14|k>>>18)^k>>>3)+a[p-7]+((l<<15|l>>>17)^(l<<13|l>>>19)^l>>>10)+a[p-16]}k=q+((h<<26|h>>>6)^(h<<21|h>>>11)^(h<<7|h>>>25))+(h&m^~h&n)+r[p]+a[p];l=((e<<30|e>>>2)^(e<<19|e>>>13)^(e<<10|e>>>22))+(e&f^e&g^f&g);q=n;n=m;m=h;h=j+k|0;j=g;g=f;f=e;e=k+l|0}b[0]=b[0]+e|0;b[1]=b[1]+f|0;b[2]=b[2]+g|0;b[3]=b[3]+j|0;b[4]=b[4]+h|0;b[5]=b[5]+m|0;b[6]=b[6]+n|0;b[7]=b[7]+q|0},_doFinalize:function(){var a=this._data,d=a.words,b=8*this._nDataBytes,e=8*a.sigBytes;
d[e>>>5]|=128<<24-e%32;d[(e+64>>>9<<4)+14]=h.floor(b/4294967296);d[(e+64>>>9<<4)+15]=b;a.sigBytes=4*d.length;this._process();return this._hash},clone:function(){var a=q.clone.call(this);a._hash=this._hash.clone();return a}});s.SHA256=q._createHelper(f);s.HmacSHA256=q._createHmacHelper(f)})(Math);
(function(){var h=CryptoJS,s=h.enc.Utf8;h.algo.HMAC=h.lib.Base.extend({init:function(f,g){f=this._hasher=new f.init;"string"==typeof g&&(g=s.parse(g));var h=f.blockSize,m=4*h;g.sigBytes>m&&(g=f.finalize(g));g.clamp();for(var r=this._oKey=g.clone(),l=this._iKey=g.clone(),k=r.words,n=l.words,j=0;j<h;j++)k[j]^=1549556828,n[j]^=909522486;r.sigBytes=l.sigBytes=m;this.reset()},reset:function(){var f=this._hasher;f.reset();f.update(this._iKey)},update:function(f){this._hasher.update(f);return this},finalize:function(f){var g=
this._hasher;f=g.finalize(f);g.reset();return g.finalize(this._oKey.clone().concat(f))}})})();
</script>
<script type="text/javascript">
    /*
     https://raw.githubusercontent.com/jkiss/crypto-js/master/tags/3.1.2/build/components/enc-base64-min.js
CryptoJS v3.1.2
code.google.com/p/crypto-js
(c) 2009-2013 by Jeff Mott. All rights reserved.
code.google.com/p/crypto-js/wiki/License
*/
(function(){var h=CryptoJS,j=h.lib.WordArray;h.enc.Base64={stringify:function(b){var e=b.words,f=b.sigBytes,c=this._map;b.clamp();b=[];for(var a=0;a<f;a+=3)for(var d=(e[a>>>2]>>>24-8*(a%4)&255)<<16|(e[a+1>>>2]>>>24-8*((a+1)%4)&255)<<8|e[a+2>>>2]>>>24-8*((a+2)%4)&255,g=0;4>g&&a+0.75*g<f;g++)b.push(c.charAt(d>>>6*(3-g)&63));if(e=c.charAt(64))for(;b.length%4;)b.push(e);return b.join("")},parse:function(b){var e=b.length,f=this._map,c=f.charAt(64);c&&(c=b.indexOf(c),-1!=c&&(e=c));for(var c=[],a=0,d=0;d<e;d++)if(d%4){var g=f.indexOf(b.charAt(d-1))<<2*(d%4),h=f.indexOf(b.charAt(d))>>>6-2*(d%4);c[a>>>2]|=(g|h)<<24-8*(a%4);a++}return j.create(c,a)},_map:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="}})();
</script>


<script>
  var secret="secret"




  data_time= '2017' + '-' + '12' + '-' + '11' + ' '  + '23' + ':' + '31' + ':' + '55';

  //params.content['timestamp'] = '2017' + '-' + '12' + '-' + '11' + ' '
  //                            + '23' + ':' + '31' + ':' + '55';

  var json = JSON.stringify({"timestamp":"2017-12-11 23:31:55"});
  console.log(json);

  var data1=CryptoJS.enc.Base64.stringify(CryptoJS.HmacSHA256(data_time,secret));
  console.log("Without json:");
  console.log(data1);//XZqVjDKYG8NRITjiMNXFZnNdUDb+JIr7Vm+f3CabETY=



  var data2=CryptoJS.enc.Base64.stringify(CryptoJS.HmacSHA256(json,secret));
  console.log("With json:");

  console.log(data2);//s5q78/m3wIb518O8M9x1rAM2DvG/I+Vmt0rBtb6LYS8=
</script>

2 个答案:

答案 0 :(得分:3)

来自python的

program main !implicit none include 'mpif.h' integer cols, divfx, divfy, iter integer xdiv, ydiv, info, max_iter, x_shift, y_shift integer, allocatable:: ipiv(:) double precision, allocatable :: A(:,:), Ainv(:,:) real, allocatable:: edge(:,:) double precision, allocatable :: u(:,:), f(:,:) double precision, allocatable :: u_exact(:,:) allocatable :: Left(:,:), Right(:,:) allocatable :: Top(:,:), Bottom(:,:) allocatable :: TempLeft(:,:), TempRight(:,:) allocatable :: TempTop(:,:), TempBottom(:,:) integer myid, master, numprocs, ierr, status(MPI_STATUS_SIZE) integer i, j, numsent, sender, L, T, R, B integer anstype, row, dovfx, dovfy, domx, domy, idx real dom1,dom2,buff double precision mesh(2), buffer(4), divx, divy, dx, dy, xd, yd double precision error, derror, error_norm character(len=100) :: domaindata call MPI_INIT(ierr) call MPI_COMM_RANK(MPI_COMM_WORLD, myid, ierr) call MPI_COMM_SIZE(MPI_COMM_WORLD, numprocs, ierr) master = 0 divx=0.d0 divy=0.d0 ! Input the number of divisions for domain decomposition and calculate sub-domain dimensions. open(1, file='Inputdata.dat', status='old') ! read(1,*) domx,domy ! read(1,*) dovfx,dovfy ! read(1,*) divfx,divfy write(*,*)'Starting the Program' write(*,*) "Enter the number of domain divisions in x-direction & and y-direction ( Enter 4 if you want three sub-domains)" read(1,*) domx,domy write(*,*) domx,domy write(*,*) "Total number of sub-domains for the problem" write(*,*) domx*domy write(*,*) "Enter the sub-domain overlap in x & y -direction as & a fraction of sub-domain length (multiple of 10)" read(1,*) dovfx,dovfy write(*,*) dovfx,dovfy write(*,*) "Enter the number of divisions in the overlap in & x & yas a fraction of sub-domain(multiple of 5)" read(1,*) divfx,divfy write(*,*) divfx,divfy divx=1.d0/(((1.d0/domx)/dovfx)/divfx) divy=1.d0/(((1.d0/domy)/dovfy)/divfy) write(*,*)"Total number of elemental divisions for the & problem domain (0,1) in both dimensions" write(*,*) divx, divy, divx*divy write(*,*)"Total number of nodal divisions for the problem domain" write(*,*) (divx+1)*(divy+1) ! time ! ************************** tic = MPI_Wtime(); ! Maximum number of iterations. max_iter=100 ! Mesh Size mesh(1)=1/divx mesh(2)=1/divy write(*,*) 'Element Size' write(*,*) mesh(1), mesh(2) if ( myid .eq. master ) then ! Send iteration number to subdomain and receive the error from each to ! calculate total error. write(*,*) 'still1' do 10 iter = 1,max_iter do 20 i = 1,domx*domy call MPI_SEND(iter, 1, MPI_INTEGER, i, i, MPI_COMM_WORLD, ierr) 20 continue ! Receive results obtained from sub-processor/sub-domain ! error = 0.d0 do 30 i = 1,domx*domy call MPI_RECV(d_error , 1, MPI_DOUBLE_PRECISION, i, iter, & MPI_COMM_WORLD, status, ierr) error = error + d_error 30 continue write(*,*) 'In iteration ', iter, 'cumulative error is', error*1.d0/domx/domy 10 continue ! time: ! ************* toc = MPI_Wtime(); ! Write results to output ! ************************** write(*,*) write(*,*) 'Time taken for parallel computation is: ',(toc-tic)*1000, 'miliseconds' else !************************************ Slaves receive mesh size for discretization ****************************************************** write(*,*) 'iter', iter write(*,*) 'myid', myid ! Slaves receive corners, then creates a Cartesian grid for finite ! difference until done message received, for one iteration. ! This is done for the first iteration ! Get Domain ID : if (myid.gt.(domx*domy)) goto 200 write(*,*) 'still31' 1000 call MPI_RECV(iter, 1, MPI_INTEGER, master, MPI_ANY_TAG, MPI_COMM_WORLD, status, ierr) if (status(MPI_TAG) .eq. 0) goto 200 write(*,*) 'still4' if (iter.eq.1) then write(*,*) 'still5' dom1=domx dom2=domy allocate (edge(domx*domy,4)) ! Determining the edge matrices for each subdomain - the bounding box do j =1,domx do k=1,domy idx=(j-1)*(domx-1)+k+(j-1) buff=REAL((mod(idx-1,domx)))/domx IF (buff-((1.d0/domx)/dovfx) .gt. 0) THEN buff=buff-((1.d0/domx)/dovfx) ENDIF edge(idx,1) = buff IF ((mod(idx ,domx)) .eq. 0) THEN buff=1 ELSE buff=REAL(mod(idx ,domx))/domx ENDIF !write(*,*) buff IF (buff + ((1.d0/domx)/dovfx) .lt. 1) THEN buff=buff+((1.d0/domx)/dovfx) ENDIF edge(idx,2) = buff ! buff=REAL(floor((idx-1)/dom1))/dom1 IF (buff -((1.d0/domy)/dovfy) .gt. 0) THEN buff=buff-((1.d0/domy)/dovfy) ENDIF edge(idx,3) = buff buff=REAL(ceiling(idx/dom1))/dom1 IF (buff+((1.d0/domy)/dovfy) .lt. 1) THEN buff= buff+((1.d0/domy)/dovfy) ENDIF edge(idx,4) = buff end do end do write(*,*) myid, edge(myid,:) write(*,*) 'iter', iter call Surround_dom(myid,domx,domy,LeftC, RightC, BottomC, TopC) ! Calculate data for the matrices: Divisions in each subdomain. : xdiv=(edge(myid,2)-edge(myid,1))/mesh(1) ydiv=(edge(myid,4)-edge(myid,3))/mesh(2) dx=mesh(1) dy=mesh(2) allocate (A((xdiv-1)*(ydiv-1),(xdiv-1)*(ydiv-1))) allocate (Ainv((xdiv-1)*(ydiv-1),(xdiv-1)*(ydiv-1))) allocate (u((xdiv-1)*(ydiv-1),1),f((xdiv-1)*(ydiv-1),1)) allocate (u_exact((xdiv-1)*(ydiv-1),1)) allocate (ipiv((xdiv-1)*(ydiv-1))) allocate (Left((ydiv-1),1),Right((ydiv-1),1)) allocate (Top((xdiv-1),1), Bottom(((xdiv-1)),1)) allocate (TempLeft((ydiv-1),1),TempRight((ydiv-1),1)) allocate (TempTop((xdiv-1),1), TempBottom(((xdiv-1)),1)) Left = 0.d0; Right = 0.d0; Bottom = 0.d0; Top = 0.d0; TempLeft = 0.d0; TempRight = 0.d0; TempBottom = 0.d0; TempTop = 0.d0; A=0; endif write(*,*) 'still6' ! ****************************************************************** ! SendReceive data based on location ! ****************************************************************** if (LeftC.ne.0) then call MPI_SENDRECV(Left, ydiv - 1, MPI_DOUBLE_PRECISION, LeftC, iter, & TempLeft, ydiv - 1, MPI_DOUBLE_PRECISION, LeftC, iter, MPI_COMM_WORLD, status, ierr) end if if (RightC.ne.0) then call MPI_SENDRECV(Right, ydiv - 1, MPI_DOUBLE_PRECISION, RightC, iter, & TempRight, ydiv - 1, MPI_DOUBLE_PRECISION, RightC, iter, MPI_COMM_WORLD, status, ierr) end if if (BottomC.ne.0) then call MPI_SENDRECV(Bottom, xdiv - 1, MPI_DOUBLE_PRECISION, BottomC, iter, & TempBottom, xdiv - 1, MPI_DOUBLE_PRECISION, BottomC, iter, MPI_COMM_WORLD, status, ierr) end if if (TopC.ne.0) then call MPI_SENDRECV(Top, xdiv - 1, MPI_DOUBLE_PRECISION, TopC, iter, & TempTop, xdiv - 1, MPI_DOUBLE_PRECISION, TopC, iter, MPI_COMM_WORLD, status, ierr) end if Left = TempLeft ; Right = TempRight; Top = TempTop ; Bottom = TempBottom; write(*,*) 'still7' ! Form the coefficient matrices do i =1,(xdiv-1)*(ydiv-1) A(i,i)=-2.d0*(1.d0/(dx**2)+1.d0/(dy**2)) enddo do i=1,(xdiv-2) do j=1,(ydiv-1) A(i+(j-1)*(xdiv-1),i+(j-1)*(xdiv-1)+1)=1.d0/(dx**2) A(i+(j-1)*(xdiv-1)+1,i+(j-1)*(xdiv-1))=1.d0/(dx**2) enddo enddo do i=1,(xdiv-1) do j=1,(ydiv-2) A(i+(j-1)*(xdiv-1),i+(j)*(xdiv-1))=1.d0/(dy**2) A(i+(j)*(xdiv-1),i+(j-1)*(xdiv-1))=1.d0/(dy**2) enddo enddo write(*,*) 'still9' L=1 T=1 R=1 B=1 write(*,*) 'still10' ! Impose Boundary Conditions in F matrix do i=1,(xdiv-1)*(ydiv-1) xd = edge(myid,1) + (dx)*mod(i,(xdiv-1)) if (mod(i,xdiv-1).eq.0) xd = edge(myid,1) + (dx)*(xdiv-1) yd = edge(myid,3) + (dy)*ceiling(i*1.d0/(xdiv-1)) !if (iter.eq.1 .and. myid.eq.2) write(*,*) xd,yd u_exact(i,1) = sin(2.d0*3.1415*xd)*sin(2.d0*3.1415*yd) f(i,1) = 8.d0*3.1415*3.1415*u_exact(i,1) IF (mod(i,(xdiv-1)) .eq. 1) THEN f(i,1)= f(i,1)+Left(L,1)/dx/dx L=L+1 ENDIF IF (mod(i,(xdiv-1)) .eq. 0) THEN f(i,1)=f(i,1)+Right(R,1)/dx/dx R=R+1 ENDIF IF (i .le. (xdiv-1)) THEN f(i,1)=f(i,1)+Bottom(B,1)/dy/dy B=B+1 ENDIF IF (i .gt. (xdiv-1)*(ydiv-2)) THEN f(i,1)=f(i,1)+Top(T,1)/dy/dy T=T+1 END IF ! enddo enddo !Solve AU=F by LU factorization! write(*,*) 'still11' do i=1,(xdiv-1)*(ydiv-1) do j=1,(xdiv-1)*(ydiv-1) Ainv(i,j)=A(i,j) end do end do ! do i=1,(xdiv-1)*(ydiv-1) ! write(*,*) myid,Ainv(i,i) !end do call DGESV((xdiv-1)*(ydiv-1), 1, A, & (xdiv-1)*(ydiv-1), ipiv, f, (xdiv-1)*(ydiv-1), info) write(*,*) 'still12' call ErrorNorm(f,u_exact,(xdiv-1)*(ydiv-1),error_norm) write(*,*) 'still13' ! **************************************************** ! Update boundary conditions based on new solution: ! **************************************************** x_shift = divfx-1 ; y_shift = divfy-1 ; ! write(*,*) 'LeftC', myid,LeftC,RightC,TopC,BottomC if (LeftC.ne.0) then do 50 i = 1,ydiv - 1 Left(i,1) = f((xdiv - 1)*(i - 1) + 1 + x_shift,1) !if ((myid.eq.2).and.(iter.eq.1)) write(*,*) 'for left',i, & !(xdiv - 1)*(i - 1) + 1 + x_shift 50 continue end if if (RightC.ne.0) then do 60 i = 1,ydiv - 1 Right(i,1) = f((xdiv - 1)*i - x_shift,1) !if ((myid.eq.1).and.(iter.eq.1)) write(*,*) 'for right',i, & !(xdiv - 1)*i - x_shift 60 continue end if if (TopC.ne.0) then do 70 i = 1,xdiv - 1 Top(i,1) = f((xdiv - 1)*((ydiv - 2) - y_shift) + i,1) !if ((myid.eq.1).and.(iter.eq.1)) write(*,*) 'for top',i, & !((xdiv - 1)*((ydiv - 2) - y_shift) + i) 70 continue end if if ( BottomC.ne.0) then do 80 i = 1,xdiv - 1 Bottom(i,1) = f((xdiv - 1)*y_shift + i,1) !if ((myid.eq.3).and.(iter.eq.1)) write(*,*) 'for bottom',i, & !((xdiv - 1)*y_shift + i) 80 continue end if write(*,*) 'still14' TempLeft =Left; TempRight = Right; TempTop = Top; TempBottom = Bottom; call MPI_SEND(error_norm, 1, MPI_DOUBLE_PRECISION, master, iter, & MPI_COMM_WORLD, ierr) write(*,*) 'still15' if (iter.lt.iter_max) go to 1000 ! ********************************************************************************* ! Write solution to data file to view the results. ! ********************************************************************************* write (domaindata, "(A7,I2,A4)") "domain_",myid,".dat" open (unit=myid*10, file = domaindata) write (myid*10,*) ' VARIABLE= "X","Y","U_EXACT","U_CALC" ' do i=1,(xdiv-1)*(ydiv-1) xd = edge(myid,1) + (dx)*mod(i,(xdiv-1)) if (mod(i,xdiv-1).eq.0) xd = edge(myid,1) + (dx)*(xdiv-1) yd = edge(myid,3) + (dy)*ceiling(i*1.d0/(xdiv-1)) write (myid*10,*) xd, yd, u_exact(i,1), f(i,1) enddo write(*,*) 'still16' if (iter.eq.max_iter) go to 200 200 continue write(*,*) 'still45' endif call MPI_FINALIZE(ierr) stop end program main subroutine Surround_dom(myid,domx,domy,LeftID, RightID, BottomID, TopID) implicit none integer myid, j, k, domy, domx, BottomID, TopID, LeftID, RightID j = ceiling(1.d0*myid/domx) k = mod(myid,domx) if (k.eq.0) k = domx ! Domain on the left if(k.eq.1) then LeftID = 0 else LeftID = ((j-1)*domx + k) - 1 end if ! Domain on the Right if(k.eq.domx) then RightID = 0 else RightID = ((j-1)*domx + k) + 1 end if ! Domain on the Bottom if(j.eq.1) then BottomID = 0 else BottomID = ((j-1)*domx + k) - domx end if ! Domain on the Top if(j.eq.domy) then TopID = 0 else TopID = ((j-1)*domx + k) + domx end if return end subroutine ErrorNorm(u,u_exact,N,error_norm) implicit none double precision u(N), u_exact(N), err, error_norm integer i, N error_norm = 0.d0 do 10 i = 1,N err = (u(i) - u_exact(i)) error_norm = error_norm + err*err 10 continue error_norm = sqrt(error_norm)/(N*1.d0) return end 和来自javascript的msg_data可能略有不同。

来自python的

json

msg_data
来自javascript的

{"timestamp": "2017-12-11 23:31:55"}

json

使用可选的separator argument to json.dumps删除每个后面的默认空间:

{"timestamp":"2017-12-11 23:31:55"}

答案 1 :(得分:2)

您正在散列不同的数据。

在Python版本中,msg_data是:

{"timestamp": "2017-12-11 23:31:55"}
            ^^^

在Javascript版本中,json是:

{"timestamp":"2017-12-11 23:31:55"}
            ^^

请注意,JS版本中"timestamp":之后没有空格。并非所有JSON实现都会为相同的数据生成完全相同的输出。 (当字典中有多个键时,您会遇到更多问题 - JSON不会对键出现的顺序设置任何要求。)