/// <summary> /// Unpack and verify signed data before casting it to the supplied type. /// </summary> /// <typeparam name="T">Type in which to cast the result to.</typeparam> /// <param name="decorator">Signature decorator to unpack and verify</param> /// <returns>Resulting data cast to type T.</returns> public static T UnpackAndVerifyData <T>(SignatureDecorator decorator) { var sigDataBytes = decorator.SignatureData.GetBytesFromBase64(); var sigDataString = sigDataBytes.Skip(8).ToArray().GetUTF8String(); return(JsonConvert.DeserializeObject <T>(sigDataString)); }
/// <summary> /// Unpack and verify signed data before casting it to the supplied type. /// </summary> /// <typeparam name="T">Type in which to cast the result to.</typeparam> /// <param name="decorator">Signature decorator to unpack and verify</param> /// <returns>Resulting data cast to type T.</returns> public static async Task <T> UnpackAndVerifyAsync <T>(SignatureDecorator decorator) { if (await Crypto.VerifyAsync( theirVk: decorator.Signer, message: decorator.SignatureData.GetBytesFromBase64(), signature: decorator.Signature.GetBytesFromBase64())) { var sigDataBytes = decorator.SignatureData.GetBytesFromBase64(); var sigDataString = sigDataBytes.Skip(8).ToArray().GetUTF8String(); return(sigDataString.ToObject <T>()); } throw new AriesFrameworkException(ErrorCode.InvalidMessage, "The signed payload was invalid"); }
/// <summary> /// Sign data supplied and return a signature decorator. /// </summary> /// <typeparam name="T">Data object type to sign.</typeparam> /// <param name="agentContext">Agent context.</param> /// <param name="data">Data to sign.</param> /// <param name="signerKey">Signers verkey.</param> /// <returns>Async signature decorator.</returns> public static async Task <SignatureDecorator> SignData <T>(IAgentContext agentContext, T data, string signerKey) { var dataJson = JsonConvert.SerializeObject(data); var epocData = new byte[8]; //TODO actually put the epoc representation in here var sigData = epocData.Concat(dataJson.GetUTF8Bytes()).ToArray(); var sig = await Crypto.SignAsync(agentContext.Wallet, signerKey, sigData); var sigDecorator = new SignatureDecorator { SignatureType = DefaultSignatureType, SignatureData = sigData.ToBase64UrlString(), Signature = sig.ToBase64UrlString(), Signer = signerKey }; return(sigDecorator); }
/// <summary> /// Sign data supplied and return a signature decorator. /// </summary> /// <typeparam name="T">Data object type to sign.</typeparam> /// <param name="agentContext">Agent context.</param> /// <param name="data">Data to sign.</param> /// <param name="signerKey">Signers verkey.</param> /// <returns>Async signature decorator.</returns> public static async Task <SignatureDecorator> SignDataAsync <T>(IAgentContext agentContext, T data, string signerKey) { var dataJson = data.ToJson(); var epochData = BitConverter.GetBytes(DateTimeOffset.UtcNow.ToUnixTimeSeconds()); var sigData = epochData.Concat(dataJson.GetUTF8Bytes()).ToArray(); var sig = await Crypto.SignAsync(agentContext.Wallet, signerKey, sigData); var sigDecorator = new SignatureDecorator { SignatureType = DefaultSignatureType, SignatureData = sigData.ToBase64UrlString(), Signature = sig.ToBase64UrlString(), Signer = signerKey }; return(sigDecorator); }