public Signer(CBORObject jsonSigner) { if (jsonSigner.ContainsKey("protected")) { protectedB64 = jsonSigner["protected"].AsString(); ProtectedMap = CBORObject.FromJSONString(Encoding.UTF8.GetString(Message.base64urldecode(jsonSigner["protected"].AsString()))); if (ProtectedMap.Count == 0) { throw new JoseException("field 'protected' must be omitted for empty maps."); } } if (jsonSigner.ContainsKey("header")) { UnprotectedMap = jsonSigner["header"]; } else if (ProtectedMap.Count == 0) { throw new JoseException("One of 'protected' or 'header' must be present."); } if (!jsonSigner.ContainsKey("signature")) { throw new JoseException("Field 'signature' must be present."); } signature = Message.base64urldecode(jsonSigner["signature"].AsString()); }
static void ProcessFile(string dir, string fileName) { StreamReader file = File.OpenText(Path.Combine(RootDir, dir, fileName)); string fileText = file.ReadToEnd(); file.Close(); file.Dispose(); CBORObject control = CBORObject.FromJSONString(fileText); Directory.CreateDirectory(RootDir + "\\new\\" + dir); try { if (ProcessJSON(control)) { fileText = control.ToJSONString(); JSON j = JSON.Parse(fileText); fileText = j.Serialize(0); StreamWriter file2 = File.CreateText(RootDir + "\\new\\" + dir + "\\" + fileName); file2.Write(fileText); file2.Write("\r\n"); file2.Close(); } ValidateJSON(control); } catch (Exception e) { Console.WriteLine($"ERROR: {e}"); } }
public static Message DecodeFromString(string messageData) { CBORObject message; // We need to figure out if this is the compact or one of the JSON encoded versions. // We guess this is going to be based on the first character - either it is a '{' or something else if (messageData[0] == '{') { message = CBORObject.FromJSONString(messageData); } else { // Split the string based on periods string[] rgData = messageData.Split('.'); if (rgData.Length == 3) { message = CBORObject.NewMap(); if (rgData[1].Length > 0) { message.Add("payload", rgData[1]); } CBORObject signature = CBORObject.NewMap(); signature.Add("protected", rgData[0]); signature.Add("signature", rgData[2]); CBORObject sigs = CBORObject.NewArray(); sigs.Add(signature); message.Add("signatures", sigs); } else if (rgData.Length == 5) { message = CBORObject.NewMap(); message.Add("protected", rgData[0]); message.Add("iv", rgData[2]); message.Add("ciphertext", rgData[3]); message.Add("tag", rgData[4]); CBORObject recip = CBORObject.NewMap(); recip.Add("encrypted_key", rgData[1]); CBORObject recips = CBORObject.NewArray(); recips.Add(recip); message.Add("recipients", recips); } else { throw new JoseException("There are not the correct number of dots."); } } return(DecodeFromJSON(message)); }
static void PrintInfo(string value) { CBORObject obj = CBORObject.FromJSONString(value); Console.WriteLine($"Name: {obj["9001"].AsString()}"); obj.Remove(CBORObject.FromObject("9001")); Console.WriteLine(obj.ToString()); }
public void Encode <T>(T obj) { if (_codec._multicodec) { _stream.Write(_codec.Header, 0, _codec.Header.Length); } var json = JsonConvert.SerializeObject(obj, Formatting.None); var cbor = CBORObject.FromJSONString(json); //FromObject(obj); cbor.WriteTo(_stream); _stream.Flush(); }
static void ProcessNestedFile(string fileName) { StreamReader file = File.OpenText(fileName); string fileText = file.ReadToEnd(); file.Close(); CBORObject control = CBORObject.FromJSONString(fileText); ProcessJSON(control["sign"]); ProcessJSON(control["encrypt"]); }
public async Task EncodeAsync <T>(T obj, CancellationToken cancellationToken) { if (cancellationToken.IsCancellationRequested) { return; } if (_codec._multicodec) { await _stream.WriteAsync(_codec.Header, 0, _codec.Header.Length, cancellationToken); } var json = JsonConvert.SerializeObject(obj, Formatting.None); var cbor = CBORObject.FromJSONString(json); //FromObject(obj); cbor.WriteTo(_stream); await _stream.FlushAsync(cancellationToken); }
private static CBORObject CloneCbor(CBORObject o) { return(CBORObject.FromJSONString(o.ToJSONString())); }
#pragma warning restore CA1801 public CBORObject GetJSON(string name) { return(CBORObject.FromJSONString( this.GetString(name))); }
/// <summary> /// Parse a JSON encoded link format structure /// </summary> /// <param name="linkFormat">link data</param> /// <returns>remote resource</returns> public static RemoteResource DeserializeJson(string linkFormat) { return(DeserializeCbor(CBORObject.FromJSONString(linkFormat))); }
public static IEnumerable <WebLink> ParseJson(string linkFormat) { CBORObject links = CBORObject.FromJSONString(linkFormat); return(ParseCommon(links, null)); }
protected override void InternalDecodeFromJSON(CBORObject json) { if (json.ContainsKey("protected")) { _strProtected = json["protected"].AsString(); ProtectedMap = CBORObject.FromJSONString(Encoding.UTF8.GetString(base64urldecode(_strProtected))); if (ProtectedMap.Type != CBORType.Map || ProtectedMap.Count == 0) { throw new JoseException("field 'protected' must be a non-empty map"); } } else { ProtectedMap = CBORObject.NewMap(); } // Can be absent if (json.ContainsKey("unprotected")) { UnprotectedMap = json["unprotected"]; if (UnprotectedMap.Type != CBORType.Map || UnprotectedMap.Count == 0) { throw new JoseException("field 'unprotected' must be a non-empty map"); } } else { UnprotectedMap = CBORObject.NewMap(); } if (json.ContainsKey("iv")) { _iv = base64urldecode(json["iv"].AsString()); } else { _iv = null; } if (json.ContainsKey("aad")) { _Aad = base64urldecode(json["aad"].AsString()); _Aad = Encoding.UTF8.GetBytes(json["aad"].AsString()); } else { _Aad = null; } if (!json.ContainsKey("ciphertext")) { throw new JoseException("field 'ciphertext' must be present"); } _RgbEncrypted = base64urldecode(json["ciphertext"].AsString()); if (json.ContainsKey("tag")) { _Tag = base64urldecode(json["tag"].AsString()); } else { _Tag = null; } if (json.ContainsKey("recipients")) { CBORObject recips = json["recipients"]; if (recips.Type != CBORType.Array || recips.Count == 0) { throw new JoseException("field 'recipients' must be a non-empty array"); } bool needHeaders = (ProtectedMap.Count + UnprotectedMap.Count) == 0; for (int i = 0; i < recips.Count; i++) { Recipient recipient = new Recipient(); recipient.DecodeFromJSON(recips[i]); RecipientList.Add(recipient); if (needHeaders) { if (recipient.ProtectedMap.Count + recipient.UnprotectedMap.Count == 0) { throw new JoseException("One of protected, unprotected or headers must be present for every recipient"); } } } } else { // Look at ths as a flattened version Recipient recipient = new Recipient(); recipient.DecodeFromJSON(json); RecipientList.Add(recipient); if (recipient.ProtectedMap.Count + recipient.UnprotectedMap.Count + ProtectedMap.Count + UnprotectedMap.Count == 0) { throw new JoseException("One of protected, unprotected or headers must be present for every recipient"); } } }
void ProcessFile(FileInfo testCase) { if (testCase.Extension != ".json") { return; } if (testCase.Name[0] == '.') { return; } Debug.Print($"Working on file {testCase}"); Console.WriteLine("Working on file '" + testCase + "'"); string inputText = testCase.OpenText().ReadToEnd(); CBORObject test = CBORObject.FromJSONString(inputText); KeySet decodeKeys = new KeySet(); KeySet signKeys = new KeySet(); CBORObject input = test["input"]; CWT cwt = new CWT(); if (input.ContainsKey("encrypted")) { OneKey key = LoadKey(input["encrypted"]["key"]); cwt.EncryptionKey = key; decodeKeys.AddKey(key); } if (input.ContainsKey("mac0")) { OneKey key = LoadKey(input["mac0"]["key"]); cwt.MacKey = key; decodeKeys.AddKey(key); } if (input.ContainsKey("sign0")) { OneKey key = LoadKey(input["sign0"]["key"]); cwt.SigningKey = key; signKeys.AddKey(key.PublicKey()); } CWT cwt2 = CWT.Decode(FromHex(test["output"]["cbor"].AsString()), decodeKeys, signKeys); CBORObject token = input["token"]; foreach (CBORObject key in token.Keys) { CBORObject value = token[key]; CBORObject key2 = key; if (key.AsString().EndsWith("_hex")) { value = CBORObject.FromObject(FromHex(value.AsString())); key2 = CBORObject.FromObject(key.AsString().Substring(0, key.AsString().Length - 4)); } cwt.SetClaim(key2, value); Assert.True(cwt2.HasClaim(key2), $"Missing Claim {key2}"); Assert.AreEqual(value, cwt.GetClaim(key2)); } byte[] foo = cwt.EncodeToBytes(); cwt2 = CWT.Decode(foo, decodeKeys, signKeys); foreach (CBORObject key in token.Keys) { CBORObject value = token[key]; CBORObject key2 = key; if (key.AsString().EndsWith("_hex")) { value = CBORObject.FromObject(FromHex(value.AsString())); key2 = CBORObject.FromObject(key.AsString().Substring(0, key.AsString().Length - 4)); } Assert.True(cwt2.HasClaim(key2)); Assert.AreEqual(value, cwt.GetClaim(key2)); } }