Difference between md5sum created by c# and bash [duplicate] - c#

This question already has an answer here:
bash, md5sum behaves strange
(1 answer)
Closed 2 years ago.
I'm trying understand why when i use a linux md5sum function, like this:
#!/bin/bash
p="4c4712a4141d261ec0ca8f90379"
Etime="123456"
TOTP=$(echo $Etime$p|md5sum|cut -f1 -d' ')
echo $TOTP
I've got: ce007ddfb5eb0ccda6d4a6ddd631c563
But when i trying generate md5sum in c# using this code:
public static string Hash()
{
string p = "4c4712a4141d261ec0ca8f90379";
string Etime = "123456";
string h = Etime + p ;
string r = md5test(h);
return r;
}
public static string md5test(string testString)
{
byte[] asciiBytes = ASCIIEncoding.UTF8.GetBytes(testString);
byte[] hashedBytes = MD5CryptoServiceProvider.Create().ComputeHash(asciiBytes);
string hashedString = BitConverter.ToString(hashedBytes).Replace("-", "").ToLower();
return hashedString;
}
I've got different value: acb6242ab9ad7a969fb27f10644a0283
I think the problem is in conversion bytes to string, but i'm not sure.
Does anyone could explain where i make mistake or how to do to have expected value:ce007ddfb5eb0ccda6d4a6ddd631c563
Thanks
Wojtek

The problem is the newline at the end of the output of echo. Use echo -n or printf instead:
TOTP=$(printf %s "$Etime$p" | md5sum | cut -f1 -d' ')

I rewritten the bash part of you test:
printf '1234564c4712a4141d261ec0ca8f90379' | md5sum
# prints: acb6242ab9ad7a969fb27f10644a0283 -
printf '1234564c4712a4141d261ec0ca8f90379\n' | md5sum
# prints: ce007ddfb5eb0ccda6d4a6ddd631c563 -
The difference is, that echo append a new line character. A solution is to use echo with option -n: echo -n .... A better solution is, to use printf instead of echo: printf %s%s' "$Etime" "$p"

Related

Shell base64 decode to bytes file

i'm trying to decode a string and save it into a file following this method
C# code working successfully:
string val;
var path = #"decoded.txt";
Console.Write("Enter value:");
val = Console.ReadLine();
byte[] bytes1 = Convert.FromBase64String(val);
File.WriteAllBytes(path, bytes1);
but i dont get the same result using bash:
echo -n realm_result.json.public_key | base64 -di > temp_key_public
EDIT
the working bash mlethod:
printf '%s' {{ realm_result.json.public_key }} | base64 -d > temp_key_public
thank you #konrad-rudolph for pointing to the type of variable, it is an ansible task result containing a string.

Line Break in C# String [duplicate]

This question already has answers here:
Replace Line Breaks in a String C#
(20 answers)
Closed 3 years ago.
I'm receiving a random line break in the following C# string and can't identify why the output is on two separate lines.
I've really only tried to define the variable in two different manners. Both produce the same results.
string cmdStr = "docker images | grep -E '^prabhasgupte/webmon.*latest' | head -1 | awk '{print $3}'";
Console.Write(cmdStr);
string imageId = commands.Bash(cmdStr);
Console.Write(imageId);
string x = $"docker tag {imageId} scan_target:{imageId}";
Console.Write(x);
This is the output I'm receiving:
docker images | grep -E '^prabhasgupte/webmon.*latest' | head -1 | awk '{print $3}'
0043699b906f
docker tag 0043699b906f
scan_target:0043699b906f
public static string Bash(this string cmd)
{
var escapedArgs = cmd.Replace("\"", "\\\"");
var process = new Process()
{
StartInfo = new ProcessStartInfo
{
FileName = "/bin/bash",
Arguments = $"-c \"{escapedArgs}\"",
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true,
}
};
process.Start();
string result = process.StandardOutput.ReadToEnd();
process.WaitForExit();
return result;
}
Seems like your issue might be awk inserting a newline, which is its default behavior. I copied this from an awk-related SO question:
The ORS (output record separator) variable in AWK defaults to "\n" and is printed after every line. You can change it to " " in the BEGIN section if you want everything printed consecutively.
the reason this is happening is that imageId has a line break in it from the bash command. try trimming the result
string imageId = commands.Bash(cmdStr).TrimEnd( '\r', '\n', Environment.NewLine.ToCharArray() );

Converting C# code to php encoding issue

I try to replicate this C# code in php to get the same output(I cannot change c# code only php).And here I'm stuck
public static string HashData(string textToBeEncripted)
{
//Convert the string to a byte array
Byte[] byteDataToHash = System.Text.Encoding.Unicode.GetBytes(textToBeEncripted);
//Compute the MD5 hash algorithm
Byte[] byteHashValue = new System.Security.Cryptography.MD5CryptoServiceProvider().ComputeHash(byteDataToHash);
return System.Text.Encoding.Unicode.GetString(byteHashValue);
}
The php code that I have made till now looks like this
$a = "test";
$a = mb_convert_encoding($a, "UTF-16LE");
$a = md5($a,true);
$a = unpack('C*', $a);
var_dump($a);
//with the output
array(16) { [1]=> int(200) [2]=> int(5) [3]=> int(158) [4]=> int(46) [5]=> int(199) [6]=> int(65) [7]=> int(159) [8]=> int(89) [9]=> int(14) [10]=> int(121) [11]=> int(215) [12]=> int(241) [13]=> int(183) [14]=> int(116) [15]=> int(191) [16]=> int(230) }
As you can see the output is the same as in the C# code
But I'm stuck at the function System.Text.Encoding.Unicode.GetString().How to replicate this in php?Or there is a easier way to get the same output?(I cannot change the C# code sorry)
Edit: Based on Vasiliy Zverev answers since the php hash is different a little bit.I end up making aproximating the hash value of php with the C# hash
function validare_parola($parola,$dbHash){
$parola = mb_convert_encoding($parola, "UTF-16LE");
$parola = md5($parola, true);
$parola = mb_convert_encoding($parola, "UCS-2BE", "UCS-2LE");
$parola = bin2hex($parola);
$procent;
similar_text($dbHash,$parola,$procent);
if($procent>=90){
return true;
}else{
return false;
}
}
$parola = "testa";
$dbHash = "10095018710be2bcbbf9bba3f9d91ce8";
if(validare_parola($parola,$dbHash)){
echo 'PASSWORD CORRECT.You can log in.';
}else{
echo 'INCORRECT PASSWORD.Try again.';
}
As a side note don't use md5 for passwords use php password hashing api
Edit2: I ended up using Vasiliy Zverev solution.
Edit3: For the value "111111" there is different output in php...
Edit4: Vasily Zverev updated his solution and now is working as expected
The solution, updated:
$a = "SF0D9G9SGGF0gdsfg976590";
$a = mb_convert_encoding($a, "UTF-16LE");
$a = md5($a, true);
$res = '';
for($i=0; $i<16; $i+=2) {
// System.Text.Encoding.Unicode.GetString(byteHashValue) replaces invalid characters to 0xfffd ('я')
// while PHP to 0x003d ('?') or empty string. So replace them like C# does
$a2 = mb_convert_encoding($a[$i].$a[$i+1], "UTF-16LE","UTF-16LE"); // check if this is invalid UTF-16 character
if(strlen($a2)==0 || $a[$i]!=$a2[0]) {
// replace invalid UTF-16 character with C# like
$v = 0xfffd;
}
else {
// prepare a word (UTF-16 character)
$v = ord($a[$i+1])<<8 | ord($a[$i]);
}
// print each word without leading zeros as C# code does
$res .= sprintf("%x",$v);
}
echo($res);
Removed this variant because it was wrong. See correct code above.

.NET md5 doesn't equal md5sum.exe on unix

So I have an issue with comparing MD5 sums of the same text using the same techniques. I am using .NET MD5 tools and Unix's md5sum.exe program.
Here's my .NET code:
public void Test()
{
string str1 = "testline1";
string str2 = "testline2";
StringBuilder sb = new StringBuilder();
sb.Append(str1);
sb.Append(Constants.vbLf);
sb.Append(str2);
string hash = GetMD5Hash(sb.ToString());
//hash = 4fb435ffb8e071151c3411dd3a922460
}
public string GetMD5Hash(string text)
{
byte[] hash;
using (MD5 md5 = MD5.Create())
{
hash = md5.ComputeHash(Encoding.UTF8.GetBytes(text));
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hash.Length; i++)
{
sb.Append(hash[i].ToString("x2"));
}
return sb.ToString();
}
I have a server running a unix based OS which has md5sum.exe on it. On my server I have a file which is simply:
testline1
testline2
Let's say the file is named testFile.txt
I have tried running multiple variations of md5sum to see if I can get these values to equal to no avail.
$md5sum testFile.txt //22f877bcf8985a0f038a5b70086b955d
$echo -n $(cat testFile.txt) | md5sum //1e192bb9877cc2f20e7010d499c0a306
$echo -E $(cat testFile.txt) | md5sum //83b6873b79ca36952eb58ea05083c02e
$unix2dos testFile.txt //converting file to dos just in case line endings are issue
$md5sum testFile.txt //d302436fa25c7faaab75ceca1f1df16d
$echo -n $(cat testFile.txt) | md5sum //94f0c8f7e9820cb6eda9e911ee21b2a8
$echo -E $(cat testFile.txt) | md5sum //f39452a70621ed225057073e86f17858
I have worked at this for hours and cannot seem to get these MD5 to match. Any ideas?
Your testFile.txt has a newline at the end of the second line, whereas your .NET code is not putting a newline at the end of the second line. If you run your file through xxd, you'll be able to see the extra newline that's tripping you up.
This is probably due to the different line-breaks on each OS.
See here for how to convert one of those files to match the other's line breaks, and I bet your checksums will then match: http://en.wikipedia.org/wiki/Newline#Conversion_utilities
echo -n "root" | md5sum
The problem is newline, when to get the result in terminal, if you don't use -n, the result is wrong.
-n do not output the trailing newline

Simple regex match question

I have the following string "sometextsometextSiteId-111-aaaaasometext"
If the string contains "SiteId-111-aaaaa" I would like to get the 111-aaaaa part. (any number, any char)
"sometextsometextSiteId-111-aaaaasometext" -> 111-aaaaa
"sometextsometextSiteId-123-abcdesometext" -> 123-abcde
"sometextsometextsitId-111-aaaaasometext" -> (nothing)
"SiteId-999-QWERTPOIPOI" -> "999-QWERR"
I guess this should be possible to do?
Any hints?
Thanks Larsi
(?<=SiteId-)([0-9]+-[a-zA-Z]{5})
should capture that part.
PowerShell test:
$re = '(?<=SiteId-)([0-9]+-[a-zA-Z]{5})'
'sometextsometextSiteId-111-aaaaasometext',
"sometextsometextSiteId-123-abcdesometext",
"sometextsometextsitId-111-aaaaasometext",
"SiteId-999-QWERTPOIPOI" |
% {
$x = [regex]::Matches($_, $re)
Write-Host $_ - $x
}
yields
sometextsometextSiteId-111-aaaaasometext - 111-aaaaa
sometextsometextSiteId-123-abcdesometext - 123-abcde
sometextsometextsitId-111-aaaaasometext -
SiteId-999-QWERTPOIPOI - 999-QWERT
SiteId-(\d{3}-\D+) this should capture that.
Also you can use rubular to try your regular expressions and it has a quick regexp reference at the bottom.

Categories