Beispiel #1
0
        /// <summary>
        /// Serializes a SerializedBiscuit to a byte array
        /// </summary>
        /// <returns></returns>
        public Either <Error, byte[]> Serialize()
        {
            Format.Schema.Biscuit biscuit = new Format.Schema.Biscuit()
            {
                Signature = signature.Serialize()
            };

            for (int i = 0; i < keys.Count; i++)
            {
                biscuit.Keys.Add(ByteString.CopyFrom(keys[i].Compress().ToByteArray()));
            }

            biscuit.Authority = ByteString.CopyFrom(authority);

            for (int i = 0; i < blocks.Count; i++)
            {
                biscuit.Blocks.Add(ByteString.CopyFrom(blocks[i]));
            }

            try
            {
                using MemoryStream stream = new MemoryStream();
                biscuit.WriteTo(stream);
                byte[] data = stream.ToArray();
                return(data);
            }
            catch (IOException e)
            {
                return(new SerializationError(e.ToString()));
            }
        }
Beispiel #2
0
        /// <summary>
        /// Deserializes a SerializedBiscuit from a byte array
        /// </summary>
        /// <param name="slice"></param>
        /// <returns></returns>
        static public Either <Error, SerializedBiscuit> FromBytes(byte[] slice)
        {
            try
            {
                Format.Schema.Biscuit data = Format.Schema.Biscuit.Parser.ParseFrom(slice);

                List <RistrettoElement> keys = data.Keys
                                               .Select(key => new CompressedRistretto(key.ToByteArray()).Decompress())
                                               .ToList();

                byte[] authority = data.Authority.ToByteArray();

                List <byte[]> blocks = data.Blocks.Select(block => block.ToByteArray()).ToList();

                Either <Error, TokenSignature> signatureRes = TokenSignature.Deserialize(data.Signature);

                if (signatureRes.IsLeft)
                {
                    return(signatureRes.Left);
                }

                TokenSignature signature = signatureRes.Right;

                SerializedBiscuit biscuitResult = new SerializedBiscuit(authority, blocks, keys, signature);

                Either <Error, Void> res = biscuitResult.Verify();
                if (res.IsLeft)
                {
                    return(res.Left);
                }
                else
                {
                    return(biscuitResult);
                }
            }
            catch (InvalidProtocolBufferException e)
            {
                return(new DeserializationError(e.ToString()));
            }
            catch (InvalidEncodingException e)
            {
                return(new DeserializationError(e.ToString()));
            }
        }