Example #1
0
      private static void Pack(string postsPath, string templatePath, string key, string outputPath)
      {
          var json  = File.ReadAllText(postsPath);
          var posts = JsonConvert.DeserializeObject <NDB.Post[]>(json);

          Validate(posts);
          var nposts = new List <NanoPost>();

          foreach (var p in posts)
          {
              nposts.Add(new NanoPost(p.replyto + Encoding.UTF8.GetString(Convert.FromBase64String(p.message))));
          }
          var packed    = NanoPostPackUtil.Pack(nposts.ToArray());
          var encrypted = ByteEncryptionUtil.EncryptSalsa20(packed, key);

          Image bmp;

          if (
              templatePath.IndexOf("data:") != -1 &&
              templatePath.IndexOf("base64,") != -1 &&
              nbpack.NBPackMain.IsBase64Encoded(templatePath.Split(',')[1])
              )
          {
              Console.WriteLine("Image uploaded and dataURL found. Create bitmap from dataURL.");

              //create bitmap from dataURL, and save this as PNG-file to Upload folder.
              var base64Data = Regex.Match(templatePath, @"data:image/(?<type>.+?),(?<data>.+)").Groups["data"].Value;
              var binData    = Convert.FromBase64String(base64Data);

              using (var stream = new MemoryStream(binData))
              {
                  bmp = new Bitmap(stream);                                     //create image from dataURL

                  //save this image as PNG-file to the folder "upload"
                  //Console.WriteLine(bmp);
                  //bmp.Save("upload/" + Guid.NewGuid().ToString() + ".png", ImageFormat.Png);
                  //bmp.Dispose();
                  //Console.WriteLine("saved to \"upload\"");
              }
              //working...
          }
          else
          {
              Console.WriteLine("DataURL not found. Create bitmap from templatePath = {0}", templatePath);
              bmp = Bitmap.FromFile(templatePath);
          }

          var capacity = (bmp.Width * bmp.Height * 3) / 8 - 32;

          if (encrypted.Length > capacity)
          {
              float scale = (encrypted.Length / (float)capacity);
              Console.WriteLine("Warning: scaling image to increase capacity: " + scale.ToString("n2") + "x");
              scale = (float)Math.Sqrt(scale);
              bmp   = new Bitmap(bmp, (int)(bmp.Width * scale + 1), (int)(bmp.Height * scale + 1));
          }

          new PngStegoUtil().HideBytesInPng(bmp, outputPath, encrypted);
          return;
      }
Example #2
0
      private static void Pack(NDB.Post[] posts, string templatePath, string key, string outputPath, string address = "")
      {
          var @set = new HashSet <string>();

          Validate(posts);
          var    nposts    = new List <NanoPost>();
          string add_notif = ((address == "CreatePNG_on_lite_server")?address + ". ":"") + "Showing posts that will go to the container:";

          //NServer.NotificationHandler.Instance.Messages.Enqueue(add_notif);	//do not add notif, and add this from response.
          NServer.DbApiHandler.notifications_with_filename += add_notif + "|||";
          foreach (var p in posts)
          {
              var mess = Encoding.UTF8.GetString(Convert.FromBase64String(p.message));
              var hash = p.hash;

              if ([email protected](hash))
              {
                  @set.Add(hash);   /*add_notif = ((address == "CreatePNG_on_lite_server")?address+". ":"")+mess; //do not define this, because nothing to do with this.
                                     * NServer.NotificationHandler.Instance.Messages.Enqueue(add_notif); //do not add notif, and add this from response*/			/* NServer.DbApiHandler.notifications_with_filename += add_notif + "|||";// don't add posts-content - to returned string */
              }

              nposts.Add(new NanoPost(p.replyto + mess));
          }
          var packed    = NanoPostPackUtil.Pack(nposts.ToArray());
          var encrypted = ByteEncryptionUtil.EncryptSalsa20(packed, key);

          Image bmp;

          if (
              templatePath.IndexOf("data:") != -1 &&
              templatePath.IndexOf("base64,") != -1 &&
              nbpack.NBPackMain.IsBase64Encoded(templatePath.Split(',')[1])
              )
          {
              Console.WriteLine("Image uploaded and dataURL found. Create bitmap from dataURL.");

              //create bitmap from dataURL, and save this as PNG-file to Upload folder.
              var base64Data = Regex.Match(templatePath, @"data:image/(?<type>.+?),(?<data>.+)").Groups["data"].Value;
              var binData    = Convert.FromBase64String(base64Data);

              using (var stream = new MemoryStream(binData))
              {
                  bmp = new Bitmap(stream);                                     //create image from dataURL

                  //save this image as PNG-file to the folder "upload"
                  //Console.WriteLine(bmp);
                  //bmp.Save("upload/" + Guid.NewGuid().ToString() + ".png", ImageFormat.Png);
                  //bmp.Dispose();
                  //Console.WriteLine("saved to \"upload\"");
              }
              //working...
          }
          else
          {
              //Console.WriteLine("DataURL not found. Create bitmap from templatePath = {0}", templatePath);
              bmp = Bitmap.FromFile(templatePath);
          }
          var capacity = (bmp.Width * bmp.Height * 3) / 8 - 32;

          if (encrypted.Length > capacity)
          {
              float scale = (encrypted.Length / (float)capacity);
              Console.WriteLine("Warning: scaling image to increase capacity: " + scale.ToString("n2") + "x");
              scale = (float)Math.Sqrt(scale);
              bmp   = new Bitmap(bmp, (int)(bmp.Width * scale + 1), (int)(bmp.Height * scale + 1));
          }

          new PngStegoUtil().HideBytesInPng(bmp, outputPath, encrypted);
          return;
      }