Esempio n. 1
0
        public static KzUInt512 SHA512(this ReadOnlySpan <byte> data)
        {
            var hash = new KzUInt512();

            SHA512(data, hash.Span);
            return(hash);
        }
Esempio n. 2
0
        public static KzUInt512 HMACSHA512(this ReadOnlySpan <byte> key, ReadOnlySpan <byte> data)
        {
            var h = new KzUInt512();

            new HMACSHA512(key.ToArray()).TransformFinalBlock(data, h.Span);
            return(h);
        }
Esempio n. 3
0
        public static KzUInt512 operator ^(KzUInt512 x, KzUInt512 y)
        {
            var r = new KzUInt512();

            r.n0 = x.n0 ^ y.n0;
            r.n1 = x.n1 ^ y.n1;
            r.n2 = x.n2 ^ y.n2;
            r.n3 = x.n3 ^ y.n3;
            r.n4 = x.n4 ^ y.n4;
            r.n5 = x.n5 ^ y.n5;
            r.n6 = x.n6 ^ y.n6;
            r.n7 = x.n7 ^ y.n7;
            return(r);
        }
Esempio n. 4
0
 public IKzWriter Add(KzUInt512 v)
 {
     v.Span.CopyTo(Memory.Span.Slice(Length)); Length += 64; return(this);
 }
Esempio n. 5
0
 public IKzWriter Add(KzUInt512 v)
 {
     Length += 64; return(this);
 }
Esempio n. 6
0
 public IKzWriter Add(KzUInt512 v)
 {
     _alg.TransformBlock(v.ReadOnlySpan); return(this);
 }
Esempio n. 7
0
 public bool Equals(KzUInt512 o) => n0 == o.n0 && n1 == o.n1 && n2 == o.n2 && n3 == o.n3 && n4 == o.n4 && n5 == o.n5 && n6 == o.n6 && n7 == o.n7;
Esempio n. 8
0
 /// <summary>
 /// Sets this extended private key to be a master (depth 0) with the private key and chaincode set from the single 512 bit vout parameter.
 /// Master private key will be set to the first 256 bits.
 /// Chaincode will be set from the last 256 bits.
 /// </summary>
 /// <param name="vout">Master private key will be set to the first 256 bits. Chaincode will be set from the last 256 bits.</param>
 /// <param name="required">if not null, each key path will be verified as valid on the specified key or returns null.</param>
 /// <returns>Returns this key unless required key paths aren't valid for specified key.</returns>
 public KzExtPrivKey SetMaster(KzUInt512 vout, IEnumerable <KzKeyPath> required = null)
 {
     return(SetMaster(vout.ReadOnlySpan.Slice(0, 32).ToKzUInt256(), vout.ReadOnlySpan.Slice(32, 32).ToKzUInt256(), required));
 }
Esempio n. 9
0
 /// <summary>
 /// Returns a new extended private key to be a master (depth 0) with the private key and chaincode set from the single 512 bit vout parameter.
 /// Master private key will be set to the first 256 bits.
 /// Chaincode will be set from the last 256 bits.
 /// </summary>
 /// <param name="vout">Master private key will be set to the first 256 bits. Chaincode will be set from the last 256 bits.</param>
 /// <param name="required">if not null, each key path will be verified as valid on the specified key or returns null.</param>
 /// <returns>Returns new key unless required key paths aren't valid for specified key in which case null is returned.</returns>
 public static KzExtPrivKey Master(KzUInt512 vout, IEnumerable <KzKeyPath> required = null)
 => new KzExtPrivKey().SetMaster(vout, required);
Esempio n. 10
0
        /// <summary>
        /// Duplicates Python hashlib's pbkdf2_hmac for hash_name = 'sha512' and dklen = None
        ///
        /// Performance can be improved by precomputing _trans_36 and _trans_5c.
        /// Unlike Python's hash functions, .NET doesn't currently support copying state between blocks.
        /// This results in having to recompute hash of innerSeed and outerSeed on each iteration.
        /// </summary>
        /// <param name="password"></param>
        /// <param name="salt"></param>
        /// <param name="iterations"></param>
        /// <returns></returns>
        public static KzUInt512 pbkdf2_hmac_sha512(ReadOnlySpan <byte> password, ReadOnlySpan <byte> salt, int iterations)
        {
            if (iterations < 1)
            {
                throw new ArgumentException();
            }

            var _password = password.ToArray();

            using var inner = new SHA512Managed();
            using var outer = new SHA512Managed();

            var blocksize = 128; // match python hashlib's sha512 blocksize.

            if (_password.Length > blocksize)
            {
                inner.TransformFinalBlock(_password, 0, _password.Length);
                _password = inner.Hash;
                //inner.Initialize();
            }

            if (_password.Length < blocksize)
            {
                Array.Resize(ref _password, blocksize);
            }

            var _trans_36 = new byte[256];
            var _trans_5c = new byte[256];

            for (var i = 0; i < 256; i++)
            {
                _trans_36[i] = (byte)(i ^ 0x36);
                _trans_5c[i] = (byte)(i ^ 0x5c);
            }

            var innerSeed = _password.Select(pb => _trans_36[pb]).ToArray();
            var outerSeed = _password.Select(pb => _trans_5c[pb]).ToArray();

            var hash  = new KzUInt512();
            var xhash = new KzUInt512();

            var data = new byte[salt.Length + 4];

            salt.CopyTo(data);
            var loop = 1;

            loop.AsReadOnlySpan(bigEndian: true).CopyTo(data.AsSpan(salt.Length));
            var dataSpan = data.AsSpan();

            for (var i = 0; i < iterations; i++)
            {
                inner.TransformBlock(innerSeed);
                outer.TransformBlock(outerSeed);
                inner.TransformFinalBlock(dataSpan, hash.Span);
                outer.TransformFinalBlock(hash.Span, hash.Span);
                dataSpan = hash.Span;
                xhash    = i == 0 ? hash : xhash ^ hash;
            }

            return(xhash);
        }