public void SetMetadata(object value) { this.VerifyCompatibility(); switch (this.Version()) { case Constants.LINK_VERSION_1_0_0: try { string canonicalData = Canonicalizer.Stringify(value); LinkMeta.Data = ByteString.CopyFrom(Encoding.UTF8.GetBytes(canonicalData)); Proto.LinkMeta meta = LinkMeta; this.ALink.Meta = meta; return; } catch (Exception e) { throw new ChainscriptException(e); } default: throw new ChainscriptException(Error.LinkVersionUnknown); } }
public void ThreeTimesThreeGrayscaleAdditiveToSubtractive() { var additiveHeader = new NetpbmHeader <byte>(ImageType.PAM, 3, 3, 1, new[] { Component.White }, 255); var additive = new NetpbmImage8(additiveHeader, new[] { new byte[] { 12, 34, 56 }, new byte[] { 78, 90, 12 }, new byte[] { 34, 56, 78 } }); var canon = new Canonicalizer(); var subtractive = canon.Canonicalize(additive, GrayscaleConversion.WhiteToBlack); subtractive.LoadData(); Assert.Equal(3, subtractive.Header.Width); Assert.Equal(3, subtractive.Header.Height); Assert.Equal(255, subtractive.Header.HighestComponentValue); Assert.Equal(1, subtractive.Header.Components.Count); Assert.Equal(Component.Black, subtractive.Header.Components[0]); Assert.NotNull(subtractive.LoadedNativeRows); Assert.Equal(3, subtractive.LoadedNativeRows.Count); Assert.Equal(3, subtractive.LoadedNativeRows[0].Count); Assert.Equal(3, subtractive.LoadedNativeRows[1].Count); Assert.Equal(3, subtractive.LoadedNativeRows[2].Count); Assert.Equal(243, subtractive.LoadedNativeRows[0][0]); Assert.Equal(221, subtractive.LoadedNativeRows[0][1]); Assert.Equal(199, subtractive.LoadedNativeRows[0][2]); Assert.Equal(177, subtractive.LoadedNativeRows[1][0]); Assert.Equal(165, subtractive.LoadedNativeRows[1][1]); Assert.Equal(243, subtractive.LoadedNativeRows[1][2]); Assert.Equal(221, subtractive.LoadedNativeRows[2][0]); Assert.Equal(199, subtractive.LoadedNativeRows[2][1]); Assert.Equal(177, subtractive.LoadedNativeRows[2][2]); }
/// <summary> /// Compute the bytes that should be signed. </summary> /// <exception cref="Exception"> /// @argument version impacts how those bytes are computed. /// @argument payloadPath parts of the link that should be signed. /// @returns bytes to be signed. </exception> public byte[] SignedBytes(string version, string payloadPath) { byte[] hashedResultBytes = null; switch (version) { case Constants.SIGNATURE_VERSION_1_0_0: if (string.IsNullOrEmpty(payloadPath)) { payloadPath = "[version,data,meta]"; } string input = null; try { input = Google.Protobuf.JsonFormatter.ToDiagnosticString(this.ALink); var jmes = new JmesPath(); string result = jmes.Transform(input, payloadPath); string canonicalResult = Canonicalizer.Canonicalize(result.ToString()); byte[] payloadBytes = Encoding.UTF8.GetBytes(canonicalResult); hashedResultBytes = CryptoUtils.Sha256(payloadBytes); } catch (IOException e1) { throw new ChainscriptException(e1); } break; default: throw new ChainscriptException(Error.SignatureVersionUnknown); } return(hashedResultBytes); }
public void GetUrl(string package, string page, string expectedSuffix) { var actualUrl = Canonicalizer.GetUrl(package, page); Assert.NotNull(actualUrl); Assert.StartsWith(Canonicalizer.CloudSitePrefix, actualUrl); var actualSuffix = actualUrl[Canonicalizer.CloudSitePrefix.Length..];
public static bool IsFileRecord(Object obj) { bool isFileRecord = false; try { string json = null; if (typeof(FileRecord).IsInstanceOfType(obj)) { isFileRecord = true; } else if (obj != null) { if (obj is JObject) { json = JsonHelper.ToCanonicalJson(obj); } else if ( obj is String //assume json ) { json = Canonicalizer.Canonicalize((String)obj); } else { json = JsonHelper.ToCanonicalJson(obj); } if (json != null) { // Attempt to generate FileRecord from json. FileRecord ob = JsonHelper.FromJson <FileRecord>(json); // Validate that all of the required fields are not null // Key can be undefined if the file is unencrypted. if ( ob.Digest != null && ob.Name != null && ob.Size != 0 && ob.Mimetype != null && ob.CreatedAt != null ) { isFileRecord = true; } } } } catch (Exception) { } return(isFileRecord); }
public static bool Verify(string xml, VipNetCrytoProvider provider) { XmlDocument doc = XDocument.Parse(xml).GetXmlDocument(); var body = Canonicalizer.GetObject(doc, "#body"); var bodyHash = provider.HashData(body); XmlNamespaceManager nsManager = new XmlNamespaceManager(doc.NameTable); nsManager.AddNamespace("ds", "http://www.w3.org/2000/09/xmldsig#"); nsManager.AddNamespace("soapenv", "http://schemas.xmlsoap.org/soap/envelope/"); nsManager.AddNamespace("wsse", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"); nsManager.AddNamespace("wsu", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"); var digestValue = (doc.SelectSingleNode("//soapenv:Envelope/soapenv:Header/wsse:Security/ds:Signature/ds:SignedInfo/ds:Reference/ds:DigestValue", nsManager) as XmlElement).InnerText; if (bodyHash.Base64 != digestValue) { return(false); } var signedInfo = Canonicalizer.GetObject(doc, "#SignedInfo"); //using (var file = File.CreateText("original-signedInfo" + ".txt")) //{ // file.WriteLine(signedInfo); //} using (var file = File.OpenText("original-signedInfo" + ".txt")) { var signedInfoWithoutDs = file.ReadToEnd(); Signature sig = provider.SigningHash(provider.HashData(signedInfoWithoutDs)); } var signedInfoHash = provider.HashData(signedInfo); var signature = provider.SigningHash(signedInfoHash); var signatureValue = (doc.SelectSingleNode("//soapenv:Envelope/soapenv:Header/wsse:Security/ds:Signature/ds:SignatureValue", nsManager) as XmlElement).InnerText; var certificateBase64 = (doc.SelectSingleNode("//soapenv:Envelope/soapenv:Header/wsse:Security/wsse:BinarySecurityToken", nsManager) as XmlElement).InnerText; X509Certificate certificate = new X509Certificate(Convert.FromBase64String(certificateBase64)); //var signature = new Signature {SignedHash = signedInfoHash, Bytes = Convert.FromBase64String(signatureValue)}; signature.Bytes = Convert.FromBase64String(signatureValue); return(provider.VerifySignature(signature)); }
/// <summary> /// Logic for your function goes here. /// </summary> /// <param name="context">The HTTP context, containing the request and the response.</param> /// <returns>A task representing the asynchronous operation.</returns> public async Task HandleAsync(HttpContext context) { var request = context.Request; var response = context.Response; string package = request.Query["package"]; string page = request.Query["page"]; if (string.IsNullOrEmpty(package) || string.IsNullOrEmpty(page)) { response.StatusCode = 400; await response.WriteAsync("Error: both package and page must be specified"); } string url = Canonicalizer.GetUrl(package, page); await response.WriteAsync(url ?? ""); }
private void DoWork() { if (isWorking) { throw new InvalidOperationException(); } try { isWorking = true; param = new MiningParams( subtreeType: GetSubtreeType(), mineOrdered: cbOrdered.Checked, mineFrequent: cbMineFrequent.Checked, mineClosed: cbMineClosed.Checked, mineMaximal: cbMineMiximal.Checked, supportType: GetSupportType(), thresholdRoot: Convert.ToInt32(txtRootSupport.Text), thresholdTransaction: Convert.ToInt32(txtTransactionSupport.Text), separator: ',', backTrackSymbol: "^"); if (!param.MineOrdered) { foreach (var tree in forest) { Canonicalizer.Canonicalize(tree); PreorderIndexBuilder.BuildPreorderIndex(tree); } ShowForest(); } var trees = forest.ToList(); var rslt = CCTreeMiner.Mine(trees, param); ShowResults(rslt); } finally { isWorking = false; } }
public void SinglePixelBitmapSubtractiveToAdditive() { var additiveHeader = new NetpbmHeader <byte>(ImageType.PAM, 1, 1, 1, new[] { Component.White }, 1); var additive = new NetpbmImage8(additiveHeader, new[] { new byte[] { 1 } }); var canon = new Canonicalizer(); var subtractive = canon.Canonicalize(additive, GrayscaleConversion.WhiteToBlack); subtractive.LoadData(); Assert.Equal(1, subtractive.Header.Width); Assert.Equal(1, subtractive.Header.Height); Assert.Equal(1, subtractive.Header.HighestComponentValue); Assert.Equal(1, subtractive.Header.Components.Count); Assert.Equal(Component.Black, subtractive.Header.Components[0]); Assert.NotNull(subtractive.LoadedNativeRows); Assert.Equal(1, subtractive.LoadedNativeRows.Count); Assert.Equal(1, subtractive.LoadedNativeRows[0].Count); Assert.Equal(0, subtractive.LoadedNativeRows[0][0]); }
/// <summary> /// Set the given object as the link's metadata. </summary> /// <param name="data"> custom data to save with the link metadata. </param> /// <exception cref="ChainscriptException"> </exception> /// <exception cref="Exception"> </exception> /// public object Metadata() { this.VerifyCompatibility(); object result = null; ByteString linkMetadata = LinkMeta.Data; if (linkMetadata == null || linkMetadata.IsEmpty) { return(result); } switch (this.Version()) { case Constants.LINK_VERSION_1_0_0: return(Canonicalizer.Parse(linkMetadata.ToStringUtf8())); default: throw new ChainscriptException(Error.LinkVersionUnknown); } }
/*** * Reads both input file and expected file * Parse input file , Stringify the output * compares the out of the process to the expected file . * @param inputFile * @param expectedFile * @return * @throws IOException */ private string[] ApplyParseStringify(string inputFile, string expectedFile) { string rawInput = File.ReadAllText(inputFile, Encoding.UTF8).Trim(); string expected = null; if (expectedFile != null) { expected = File.ReadAllText(expectedFile, Encoding.UTF8).Trim(); } string actual = Canonicalizer.Canonicalize(rawInput); string parent = Path.GetDirectoryName(inputFile); File.WriteAllBytes(Path.Combine(parent, "output.json"), Encoding.UTF8.GetBytes(actual)); return(new String[] { rawInput, expected, actual }); }
public static bool IsFileRecord(Object obj) { bool isFileRecord = false; try { string json = null; if (typeof(FileRecord).IsInstanceOfType(obj)) { isFileRecord = true; } else if (obj != null) { if (obj is JObject) { json = JsonHelper.ToCanonicalJson(obj); } else if (obj is String) //assume json { json = Canonicalizer.Canonicalize((String)obj); } else { json = JsonHelper.ToCanonicalJson(obj); } if (json != null) { //attempt to generate FileRecord from json. Object ob = JsonHelper.FromJson <FileRecord>(json); String json2 = JsonHelper.ToCanonicalJson(ob); if (json2.Equals(json)) { isFileRecord = true; } } } } catch (Exception) { } return(isFileRecord); }
/// <summary> /// gets the Data /// </summary> /// <returns></returns> public object Data() { /// <summary> /// The link data (business logic details about the execution of a process step). </summary> /// <exception cref="Exception"> /// @returns the object containing the link details. </exception> this.VerifyCompatibility(); if (this.ALink.Data == null || this.ALink.Data.IsEmpty) { return(null); } switch (this.Version()) { case Constants.LINK_VERSION_1_0_0: return(Canonicalizer.Parse(this.ALink.Data.ToStringUtf8())); default: throw new ChainscriptException(Error.LinkVersionUnknown); } }
public void FiveTimesFiveWCMYToCMYK() { var additiveHeader = new NetpbmHeader <byte>(ImageType.PAM, 5, 5, 1, new[] { Component.White, Component.Cyan, Component.Magenta, Component.Yellow }, 255); var additive = new NetpbmImage8(additiveHeader, new[] { new byte[] { 12, 34, 56, 78, 90, 12, 34, 56, 78, 90, 12, 34, 56, 78, 90, 12, 34, 56, 78, 90 }, new byte[] { 34, 56, 78, 90, 12, 34, 56, 78, 90, 12, 34, 56, 78, 90, 12, 34, 56, 78, 90, 12 }, new byte[] { 56, 78, 90, 12, 34, 56, 78, 90, 12, 34, 56, 78, 90, 12, 34, 56, 78, 90, 12, 34 }, new byte[] { 78, 90, 12, 34, 56, 78, 90, 12, 34, 56, 78, 90, 12, 34, 56, 78, 90, 12, 34, 56 }, new byte[] { 90, 12, 34, 56, 78, 90, 12, 34, 56, 78, 90, 12, 34, 56, 78, 90, 12, 34, 56, 78 } }); var referenceValues = new[] { new byte[] { 34, 56, 78, 243, 12, 34, 56, 165, 90, 12, 34, 177, 78, 90, 12, 199, 56, 78, 90, 221 }, new byte[] { 56, 78, 90, 221, 34, 56, 78, 243, 12, 34, 56, 165, 90, 12, 34, 177, 78, 90, 12, 199 }, new byte[] { 78, 90, 12, 199, 56, 78, 90, 221, 34, 56, 78, 243, 12, 34, 56, 165, 90, 12, 34, 177 }, new byte[] { 90, 12, 34, 177, 78, 90, 12, 199, 56, 78, 90, 221, 34, 56, 78, 243, 12, 34, 56, 165 }, new byte[] { 12, 34, 56, 165, 90, 12, 34, 177, 78, 90, 12, 199, 56, 78, 90, 221, 34, 56, 78, 243 } }; var canon = new Canonicalizer(); var subtractive = canon.Canonicalize(additive, GrayscaleConversion.WhiteToBlack); subtractive.LoadData(); Assert.Equal(5, subtractive.Header.Width); Assert.Equal(5, subtractive.Header.Height); Assert.Equal(255, subtractive.Header.HighestComponentValue); Assert.Equal(4, subtractive.Header.Components.Count); Assert.Equal(Component.Cyan, subtractive.Header.Components[0]); Assert.Equal(Component.Magenta, subtractive.Header.Components[1]); Assert.Equal(Component.Yellow, subtractive.Header.Components[2]); Assert.Equal(Component.Black, subtractive.Header.Components[3]); Assert.NotNull(subtractive.LoadedNativeRows); Assert.Equal(5, subtractive.LoadedNativeRows.Count); Assert.Equal(20, subtractive.LoadedNativeRows[0].Count); Assert.Equal(20, subtractive.LoadedNativeRows[1].Count); Assert.Equal(20, subtractive.LoadedNativeRows[2].Count); Assert.Equal(20, subtractive.LoadedNativeRows[3].Count); Assert.Equal(20, subtractive.LoadedNativeRows[4].Count); Assert.Equal((IEnumerable <byte>)referenceValues[0], (IEnumerable <byte>)subtractive.LoadedNativeRows[0]); Assert.Equal((IEnumerable <byte>)referenceValues[1], (IEnumerable <byte>)subtractive.LoadedNativeRows[1]); Assert.Equal((IEnumerable <byte>)referenceValues[2], (IEnumerable <byte>)subtractive.LoadedNativeRows[2]); Assert.Equal((IEnumerable <byte>)referenceValues[3], (IEnumerable <byte>)subtractive.LoadedNativeRows[3]); Assert.Equal((IEnumerable <byte>)referenceValues[4], (IEnumerable <byte>)subtractive.LoadedNativeRows[4]); }
public void ThreeTimesThreeBGRToRGB() { var bgrHeader = new NetpbmHeader <byte>(ImageType.PAM, 3, 3, 1, new[] { Component.Blue, Component.Green, Component.Red }, 255); var bgr = new NetpbmImage8(bgrHeader, new[] { new byte[] { 255, 0, 0, 0, 255, 0, 0, 0, 255 }, new byte[] { 0, 255, 0, 0, 0, 255, 255, 0, 0 }, new byte[] { 0, 0, 255, 255, 0, 0, 0, 255, 0 } }); var referenceValues = new[] { new byte[] { 0, 0, 255, 0, 255, 0, 255, 0, 0 }, new byte[] { 0, 255, 0, 255, 0, 0, 0, 0, 255 }, new byte[] { 255, 0, 0, 0, 0, 255, 0, 255, 0 } }; var canon = new Canonicalizer(); var rgb = canon.Canonicalize(bgr, GrayscaleConversion.WhiteToBlack); rgb.LoadData(); Assert.Equal(3, rgb.Header.Width); Assert.Equal(3, rgb.Header.Height); Assert.Equal(255, rgb.Header.HighestComponentValue); Assert.Equal(3, rgb.Header.Components.Count); Assert.Equal(Component.Red, rgb.Header.Components[0]); Assert.Equal(Component.Green, rgb.Header.Components[1]); Assert.Equal(Component.Blue, rgb.Header.Components[2]); Assert.NotNull(rgb.LoadedNativeRows); Assert.Equal(3, rgb.LoadedNativeRows.Count); Assert.Equal(9, rgb.LoadedNativeRows[0].Count); Assert.Equal(9, rgb.LoadedNativeRows[1].Count); Assert.Equal(9, rgb.LoadedNativeRows[2].Count); Assert.Equal((IEnumerable <byte>)referenceValues[0], (IEnumerable <byte>)rgb.LoadedNativeRows[0]); Assert.Equal((IEnumerable <byte>)referenceValues[1], (IEnumerable <byte>)rgb.LoadedNativeRows[1]); Assert.Equal((IEnumerable <byte>)referenceValues[2], (IEnumerable <byte>)rgb.LoadedNativeRows[2]); }
public static Boolean isFileWrapper(Object obj) { bool isFileWrapper = false; try { string json = null; if (typeof(FileWrapper).IsInstanceOfType(obj)) { isFileWrapper = true; } else if (obj != null) { if (obj is JObject) { json = JsonHelper.ToCanonicalJson(obj); } else if (obj is String) //assume json { json = Canonicalizer.Canonicalize((String)obj); } else { json = JsonHelper.ToCanonicalJson(obj); } if (json != null) { Object ob = null; //attempt to generate FileWrapper from json. try { if (json.ToUpper().Contains("PATH")) { ob = JsonHelper.FromJson <FilePathWrapper>(json); } else if (json.ToUpper().Contains("FILEINFO")) { ob = JsonHelper.FromJson <FileBlobWrapper>(json); } else if (json.ToUpper().Contains("FILE")) { ob = JsonHelper.FromJson <BrowserFileWrapper>(json); } } catch (Exception e) { //obj can not be converted } if (ob != null) { String json2 = JsonHelper.ToCanonicalJson(ob); if (json2.Equals(json)) { isFileWrapper = true; } } } } } catch (Exception ex) { Console.WriteLine(ex.Message); } return(isFileWrapper); }
public static string ToCanonicalJson(Object src) { string json = ToJson(src); return(Canonicalizer.Canonicalize(json)); }