I have to implement the below code in my Java application, but I am unable to find the jar file for HexUtils.bytesToHex(). Where do I find that?
byte[] data = cleartext.getBytes(ENCODING);
md.update(data);
byte[] digestedByteArray = md.digest();
// Convert digested bytes to 2 chars Hex Encoding
md5String = HexUtils.bytesToHex(digestedByteArray) );
Not sure that I got what does this code , but seems the same can be done with help of org.apache.commons.codec.binary.Hex.encodeHexString(final byte[] data) (commons-codec-*.jar). This one very easy to find
Related
I am updating web.config file of Asp.net mvc dynamically while installation using installshiled script.
It works correctly on all machines; however it generates ??? charaters on Chinese machine at the start of web.config file like below.
???<?xml version="1.0" encoding="utf-8"?>
Please suggest how this problem can be please.
Below is the Installshield code
Using installscript I am finding connection string place holder and replacing that with connection string generated while installation.
szIniFile = INSTALLDIR^"AppDir\\Web.config";
szSearchStr = "[COONECTIONSTRING]";
FindAndReplaceInFile(szIniFile, szSearchStr,strWebConString);
function FindAndReplaceInFile(szFile, szSearchStr,szReplaceStr)
STRING szReturnLine,szString, szSecPart,szFirstPart,svString,szArchive;
NUMBER nResult,nSubPos,nSearchStrLen,nLineNumber;
begin
nSearchStrLen = StrLength(szSearchStr);
nResult=FileGrep (szFile, szSearchStr, szReturnLine, nLineNumber,
RESTART) ;
NumToStr ( svString, nResult );
while (nResult=0)
nSubPos = StrFind(szReturnLine, szSearchStr); //get position of szSearchStr
StrSub (szFirstPart, szReturnLine, 0, nSubPos);
StrSub (szSecPart, szReturnLine, nSubPos+nSearchStrLen, StrLength(szReturnLine));
szString="";
szString = szFirstPart+szReplaceStr+szSecPart;
FileInsertLine (szFile, szString, nLineNumber, REPLACE);
nLineNumber = nLineNumber + 1;
nResult=FileGrep (szFile, szSearchStr, szReturnLine, nLineNumber,CONTINUE) ;
endwhile;
end;
You had a byte order marker (BOM) at the start of the file.
I suspect that what has happened is you opened a UTF8 encoded file in as a different encoding. This misread the unnecessary BOM and corrupted it. When you saved it, the unknown character markers replaced the BOM.
To rectify this, you need to encode your config as UTF8 without a BOM. Edits should then be safe, unless you have other characters outside the ASCII range in your file.
Good morning,
I need to Encryption a file in a Hash mode.
I look this library to use
HashLibrary
My Test with local C# 256 Method and HashLib Method get Differents Results,
FileStream fileStream;
SHA256 sha256 = SHA256Managed.Create();
fileStream = new FileStream(localPath, FileMode.Open);
fileStream.Position = 0;
///using System.Security.Cryptography;
byte[] hashValue = sha256.ComputeHash(fileStream);
string hash = ByteArrayToString(hashValue);
#region using HashLib;
//Run Hash
IHash hash256 = HashFactory.Crypto.CreateSHA256();
HashResult result256 = hash256.ComputeStream(fileStream);
byte[] bytearray = result256.GetBytes();
string stringtest = result256.ToString();
stringtest = result256.ToString().Replace("-", "");
#endregion
Result of First Method
byte[] hashValue = 94,171,27,169,32,82,120,2,177,84,58,6,216,77,110,239,85,282,75,159,183,85,70,208,22,146,201,22,47,122,153,74
string hash = 5EAB1BA920527802B1543A06D84D6EEF55FC4B9FB75546D01692C9162F7A994A
Result of Second Method with HashLib
var bytearray = 227,176,196,66,152,252,28,20,154,251,244,200,153,11,185,36,39,174,65,228,100,155,147,76,164,149,153,27,120,82,184,85
var stringtest = E3B0C442-98FC1C14-9AFBF4C8-996FB924-27AE41E4-649B934C-A495991B-7852B855
Someone can help me? i dont understand what is the problem, why result are differents?.
I start to use this library becouse i cant sha224, sha1 with System.Security.Cryptography
You already have read everything from the stream, so you're at the end of the stream. Recreate it to perform the testing. The second string is the well known hash over an empty array (aka nuthin').
Sometimes it is easy to check your output against well known tools such as sha256sum. For instance, the empty array can be tested like this, given a normal *nix shell (e.g. Cygwin or the Windows Subsystem for Linux I suppose):
$ dd count=0 status=none | sha256sum -b | awk '{print $1}'
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
How can I convert an image to array of bytes using ImageSharp library?
Can ImageSharp library also suggest/provide RotateMode and FlipMode based on EXIF Orientation?
If you are looking to convert the raw pixels into a byte[] you do the following.
var bytes = image.SavePixelData()
If you are looking to convert the encoded stream as a byte[] (which I suspect is what you are looking for). You do this.
using (var ms = new MemoryStream())
{
image.Save(ms, imageFormat);
return ms.ToArray();
}
For those who look after 2020:
SixLabors seems to like change in naming and adding abstraction layers, so...
Now to get a raw byte data you do the following steps.
Get MemoryGroup of an image using GetPixelMemoryGroup() method.
Converting it into array (because GetPixelMemoryGroup() returns a interface) and taking first element (if somebody tells me why they did that, i'll appreciate).
From System.Memory<TPixel> get a Span and then do stuff in old way.
(i prefer solution from #Majid comment)
So the code looks something line this:
var _IMemoryGroup = image.GetPixelMemoryGroup();
var _MemoryGroup = _IMemoryGroup.ToArray()[0];
var PixelData = MemoryMarshal.AsBytes(_MemoryGroup.Span).ToArray();
ofc you don't have to split this into variables and you can do this in one line of code. I did it just for clarification purposes. This solution only viable as for 06 Sep 2020
What I want to do is the following:
I encrypted the ".prototxt" and ".caffemodel" file, so the files are not readable and the parameters not visible. During my program I decrypt the file and store the result as a string. But now I need to set the layers in my caffe network.
Is there a method to set the caffe network layers with the parameters from my string? Same for the layers in the trained network? Something comparable with the source code below (I know this source code would not work)?
shared_ptr<Net<float> > net_;
string modelString;
string trainedString;
//Decryption stuff
net_.reset(new Net<float>(modelString, TEST));
net_->CopyTrainedLayersFrom(trainedString);
Thank you a lot.
You could initialize a NetParameter class directly using the Protocol Buffer API of the NetParameter class (you'll need to include caffe/proto/caffe.pb.h):
bool ParseFromString(const string& data);
and then use it to initialize a Net class using the following constructor:
explicit Net(const NetParameter& param, const Net* root_net = NULL);
and for copying the weights:
void CopyTrainedLayersFrom(const NetParameter& param);
It's important to note that the above method requires that the string variable contains binary-formatted protobuffer and not the text-format. While the caffemodel outputted by Caffe is already in binary format, you'll have to convert the prototxt file to binary format as well, but you could do that using the protoc command-line program combined with the --encode flag.
For a more information, I'd suggest you'll look in the Protocol-Buffer's website: https://developers.google.com/protocol-buffers/
Loading net model from text format (without converting with protoc) can be done by following:
#include <google/protobuf/text_format.h>
// [...]
NetParameter net_parameter;
bool success = google::protobuf::TextFormat::ParseFromString(model, &net_parameter);
if (success){
net_parameter.mutable_state()->set_phase(TEST);
net_.reset(new Net<float>(net_parameter));
}
I have a Data table which a has a column for images.
I am using the following code to read the image
byte[] Logo = (byte[])dt_Info.Rows[0]["Logo"];
But it got this error :
Unable to cast object of type 'System.String' to type 'System.Byte[]'.
In addition I Save the image binary code in ''bytes".
When I Write this Code , it works correctly.
byte[] Logo = bytes;
What Should I do to handle the error and read image from Data-table?
byte[] array = Encoding.ASCII.GetBytes(dt_Info.Rows[0]["Logo"].ToString());
you can get byte array like this
string s = (string)dt_Info.Rows[0]["Logo"];
byte[] data = System.Text.Encoding.ASCII.GetBytes(s);