コード例 #1
0
ファイル: SM3.cs プロジェクト: xingx001/Vive.Crypto
        public static string Hash(string str)
        {
            SM3Digest sm3 = new SM3Digest();

            byte[] md   = new byte[sm3.getDigestSize()];
            byte[] msg1 = Encoding.UTF8.GetBytes(str);
            sm3.update(msg1, 0, msg1.Length);
            sm3.doFinal(md, 0);
            string s = Encoding.UTF8.GetString(Hex.Encode(md));

            return(s.ToUpper());
        }
コード例 #2
0
        public virtual byte[] Sm2GetZ(byte[] userId, ECPoint userKey)
        {
            //SM3Digest sm3 = new SM3Digest();
            //byte[] p;
            //// userId Length
            //int len = userId.Length * 8;
            //sm3.Update((byte)(len >> 8 & 0x00ff));
            //sm3.Update((byte)(len & 0x00ff));

            //// userId
            //sm3.BlockUpdate(userId, 0, userId.Length);

            //// a,b
            //p = ecc_a.ToByteArray();
            //sm3.BlockUpdate(p, 0, p.Length);
            //p = ecc_b.ToByteArray();
            //sm3.BlockUpdate(p, 0, p.Length);
            //// gx,gy
            //p = ecc_gx.ToByteArray();
            //sm3.BlockUpdate(p, 0, p.Length);
            //p = ecc_gy.ToByteArray();
            //sm3.BlockUpdate(p, 0, p.Length);

            //// x,y
            //p = userKey.XCoord.ToBigInteger().ToByteArray();
            //sm3.BlockUpdate(p, 0, p.Length);
            //p = userKey.YCoord.ToBigInteger().ToByteArray();
            //sm3.BlockUpdate(p, 0, p.Length);

            //// Z
            //byte[] md = new byte[sm3.GetDigestSize()];
            //sm3.DoFinal(md, 0);

            //  return md;

            SM3Digest sm3 = new SM3Digest();

            int len = userId.Length * 8;

            sm3.update((byte)(len >> 8 & 0xFF));
            sm3.update((byte)(len & 0xFF));
            sm3.update(userId, 0, userId.Length);

            byte[] p = byteConvert32Bytes(this.ecc_a);
            sm3.update(p, 0, p.Length);

            p = byteConvert32Bytes(this.ecc_b);
            sm3.update(p, 0, p.Length);

            p = byteConvert32Bytes(this.ecc_gx);
            sm3.update(p, 0, p.Length);

            p = byteConvert32Bytes(this.ecc_gy);
            sm3.update(p, 0, p.Length);

            p = byteConvert32Bytes(userKey.Normalize().XCoord.ToBigInteger());
            sm3.update(p, 0, p.Length);

            p = byteConvert32Bytes(userKey.Normalize().YCoord.ToBigInteger());
            sm3.update(p, 0, p.Length);

            byte[] md = new byte[sm3.getDigestSize()];
            sm3.doFinal(md, 0);
            return(md);
        }