Hi,
In a nutshell, why am I getting the results I am getting? I would have
expected a call to method1( n) to return an 'n' length string.
At first I thought it was a problem in my function, but after finding an
article showing how to do the salt, I noticed, they had basically the same
code as me. So obviously, I'm so lost, I didn't even know I was lost.
Can somebody please direct me to an online article explaining what I don't
understand.
Thanks
-- class --
class Class1
{
static void Main(string[] args)
{
for( int len = 10; len <30; len++)
{
string s = Class1.Method1( len);
Console.Out.WriteLine( "Method1({0}) results : {1} and len={2}", len,
s, s.Length);
}
Console.Out.WriteLine( "press enter to exit");
Console.In.Read();
}
// from internet
public static string Method1( int saltLen)
{
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
byte[] buff = new byte[saltLen];
rng.GetBytes(buff);
return Convert.ToBase64String(buff);
}
}
-- output --
Method1(10) results : vQERIYXmGC7K/w== and len=16
Method1(11) results : RaA9HXz31CjigAg= and len=16
Method1(12) results : g3CbTZskaCCUjFDU and len=16
Method1(13) results : GInzz5LJpUXkOnaJ4Q== and len=20
Method1(14) results : UyTlZoOACCSTswc6IBI= and len=20
Method1(15) results : 8bJ3AvmJRqZy2Sw3wy+8 and len=20
Method1(16) results : 9PeRWzfrhGjzl27WOa1iyw== and len=24
Method1(17) results : bUHbbeLpLFQrn/RDzf01Ny0= and len=24
Method1(18) results : 3ZBiY++Vcd8reojolOmfPpfg and len=24
Method1(19) results : 3ToL3JDa9naqJrHlQQm/kUg8Vg== and len=28
Method1(20) results : OINljarFIM2uAypPLzMl6qfLN60= and len=28
Method1(21) results : R3PGSjLnhTrAxRrUVFXpL1DCrkNH and len=28
Method1(22) results : L/XiRyRWkU9+GrQnGkB5hzvtgA5LPQ== and len=32
Method1(23) results : YSwyvS2X5C7I0zxdNL8nSa7h/6T1fAM= and len=32
Method1(24) results : kHkaClhr+XutyhFUX0j3I/Hu9iko0Cvm and len=32
Method1(25) results : a5wFU26nON85AjkVQ+2A+1g+NqTq4UCuzw== and len=36
Method1(26) results : 1gPsznq6t4x1ME0ohfiZ2FtV0qBUWfbOtzE= and len=36
Method1(27) results : 03gkSPvg9oLS7u8Ic3XsGVZ90+R9WmF0uuKz and len=36
Method1(28) results : SMS+B8+xBz2xapyrg572Awrf0W9jydbExXTs+g== and len=40
Method1(29) results : m+LkKfPVOaJi7J2giJpDqFGEJoxu4WgLBK/qyC4= and len=40
press enter to exit 5 1303
When using Base64, a byte may be converted to more than one character, after
all base64 only uses "readable" characters. The relationship between the
length of the byte array and the length of the string depends on the
contents of the byte array.
--
Francisco Padron www.chartfx.com
"John" <Pl**********@To.the.group.com> wrote in message
news:ui**************@TK2MSFTNGP12.phx.gbl... Hi,
In a nutshell, why am I getting the results I am getting? I would have expected a call to method1( n) to return an 'n' length string.
At first I thought it was a problem in my function, but after finding an article showing how to do the salt, I noticed, they had basically the same code as me. So obviously, I'm so lost, I didn't even know I was lost.
Can somebody please direct me to an online article explaining what I don't understand.
Thanks
-- class -- class Class1 { static void Main(string[] args) { for( int len = 10; len <30; len++) { string s = Class1.Method1( len); Console.Out.WriteLine( "Method1({0}) results : {1} and len={2}", len, s, s.Length); }
Console.Out.WriteLine( "press enter to exit"); Console.In.Read(); }
// from internet public static string Method1( int saltLen) { RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider(); byte[] buff = new byte[saltLen]; rng.GetBytes(buff); return Convert.ToBase64String(buff); } }
-- output -- Method1(10) results : vQERIYXmGC7K/w== and len=16 Method1(11) results : RaA9HXz31CjigAg= and len=16 Method1(12) results : g3CbTZskaCCUjFDU and len=16 Method1(13) results : GInzz5LJpUXkOnaJ4Q== and len=20 Method1(14) results : UyTlZoOACCSTswc6IBI= and len=20 Method1(15) results : 8bJ3AvmJRqZy2Sw3wy+8 and len=20 Method1(16) results : 9PeRWzfrhGjzl27WOa1iyw== and len=24 Method1(17) results : bUHbbeLpLFQrn/RDzf01Ny0= and len=24 Method1(18) results : 3ZBiY++Vcd8reojolOmfPpfg and len=24 Method1(19) results : 3ToL3JDa9naqJrHlQQm/kUg8Vg== and len=28 Method1(20) results : OINljarFIM2uAypPLzMl6qfLN60= and len=28 Method1(21) results : R3PGSjLnhTrAxRrUVFXpL1DCrkNH and len=28 Method1(22) results : L/XiRyRWkU9+GrQnGkB5hzvtgA5LPQ== and len=32 Method1(23) results : YSwyvS2X5C7I0zxdNL8nSa7h/6T1fAM= and len=32 Method1(24) results : kHkaClhr+XutyhFUX0j3I/Hu9iko0Cvm and len=32 Method1(25) results : a5wFU26nON85AjkVQ+2A+1g+NqTq4UCuzw== and len=36 Method1(26) results : 1gPsznq6t4x1ME0ohfiZ2FtV0qBUWfbOtzE= and len=36 Method1(27) results : 03gkSPvg9oLS7u8Ic3XsGVZ90+R9WmF0uuKz and len=36 Method1(28) results : SMS+B8+xBz2xapyrg572Awrf0W9jydbExXTs+g== and len=40 Method1(29) results : m+LkKfPVOaJi7J2giJpDqFGEJoxu4WgLBK/qyC4= and len=40 press enter to exit
John <Pl**********@To.the.group.com> wrote: In a nutshell, why am I getting the results I am getting? I would have expected a call to method1( n) to return an 'n' length string.
At first I thought it was a problem in my function, but after finding an article showing how to do the salt, I noticed, they had basically the same code as me. So obviously, I'm so lost, I didn't even know I was lost.
Can somebody please direct me to an online article explaining what I don't understand.
It sounds like it's what Convert.ToBase64String does that's the
problem. It Base64 encodes an arbitrary byte array - and the results of
Base64 encoding are always 4/3 * size of original data, rounded up to
the nearest 4, in characters.
Now, what exactly are you trying to do? If you just need the salt as a
binary value and you're encoding it for transport, that's fine -
decoding it from base64 will give you the right results. If, however,
you want a random string of a given length (and from a given set of
characters) this isn't the best way of doing it.
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om... Now, what exactly are you trying to do? If you just need the salt as a binary value and you're encoding it for transport, that's fine - decoding it from base64 will give you the right results. If, however, you want a random string of a given length (and from a given set of characters) this isn't the best way of doing it.
Thanks Jon,
Basically, I am trying to create a salt-password secenario similar to the
one in the Salting a Hash section of http://www.dotnetjunkies.com/Tutoria...28327FF14.dcik
If that's not the best way to create a random string with a given length,
what is?
Thanks again,
John
John <Pl**********@To.the.group.com> wrote: Basically, I am trying to create a salt-password secenario similar to the one in the Salting a Hash section of http://www.dotnetjunkies.com/Tutoria...28327FF14.dcik
If that's not the best way to create a random string with a given length, what is?
Well, something like:
[ThreadStatic]
static Random random;
public static string GenerateRandomString (string characters,
int length)
{
if (random==null)
{
// Help to give each thread a different RNG seed
random = new Random(Environment.TickCount ^
Thread.CurrentThread.GetHashCode());
}
char[] ret = new char[length];
for (int i=0; i < length; i++)
{
ret[i] = characters[random.Next(characters.Length)];
}
return new string (ret);
}
You pass in the character set you want to use, and the length.
Considerably more flexible than using base64.
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jon Skeet [C# MVP] <sk***@pobox.com> wrote: John <Pl**********@To.the.group.com> wrote: Basically, I am trying to create a salt-password secenario similar to the one in the Salting a Hash section of http://www.dotnetjunkies.com/Tutoria...28327FF14.dcik
If that's not the best way to create a random string with a given length, what is?
Well, something like:
<snip>
Having had a closer look at the article, it's suggesting using
RNGCryptoServiceProvider for extra security. If this is important to
you but you still want the flexibility of the method I showed, you can
reasonably easily change my code to use the bytes provided by the
RNGCryptoServiceProvider as indexes into the character set rather than
using System.Random. (The use of a thread local will reduce the risk of
getting identical salts anyway, but RNGCryptoServiceProvider is
probably still safer.)
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: clv |
last post by:
Hello:
I need to rescue the value of a variable type application from a file ".vb",
when making
"j = Application("test")" throws an error to me, if I the same make from a
pagina aspx I do not...
|
by: John |
last post by:
Hi,
In a nutshell, why am I getting the results I am getting? I would have
expected a call to method1( n) to return an 'n' length string.
At first I thought it was a problem in my function,...
|
by: satyanagendra |
last post by:
Hi
I have problem with rescue mode in fedora 7
My system configeration is
P4 dual core
160 GB hard disk
1GB RAM
when i am installing fedora 7 version in my system it is installed and it is...
|
by: arnuld |
last post by:
I searched the c.l.c archives provided by Google as Google Groups with
"word input" as the key words and did not come up with anything good.
C++ has std::string for taking a word as input from...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
| |