473,386 Members | 2,050 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

I want to clear "immutable" string contents

Hi All!

I want to clear the string contents from sensitive information
such as passwords, and etc.

It's always a case that password will appear as string at some point
or another. And i feel uneasy leaving it hanging in memory indefinitely
(especially in case when string is Interned).

So at leats for the case when string is not interned i propose:

string pass = Console.ReadLine();
if (string.IsInterned(pass) == null)
{
unsafe
{
fixed(void* pv = pass)
{
char* pb = (char*)pv;
for(int i =0; i<pass.Length; ++i)
pb[i] = '0';
}
}
}
Console.WriteLine(pass);

Note: explicit RuntimeHelpers.OffsetToStringData is not needed.

What do you all think about this?
Jul 19 '05 #1
6 2801
Hi,

Since you know that strings are immutable, you can't clear or modify them in
any way (in theory).

Why not use a char array instead to store your password chars? It is at your
own disposal to create the array and destroy it. A few chars won't take up
too much memory.

Edward

"cppdev" <cp*****@yahoo.com> wrote in message
news:fc*************************@posting.google.co m...
Hi All!

I want to clear the string contents from sensitive information
such as passwords, and etc.

It's always a case that password will appear as string at some point
or another. And i feel uneasy leaving it hanging in memory indefinitely
(especially in case when string is Interned).

So at leats for the case when string is not interned i propose:

string pass = Console.ReadLine();
if (string.IsInterned(pass) == null)
{
unsafe
{
fixed(void* pv = pass)
{
char* pb = (char*)pv;
for(int i =0; i<pass.Length; ++i)
pb[i] = '0';
}
}
}
Console.WriteLine(pass);

Note: explicit RuntimeHelpers.OffsetToStringData is not needed.

What do you all think about this?

Jul 19 '05 #2
Hi,

I would love to use byte[] or char[],
but it's not my choice. I'm using TextControl
to get information from the user in winform.
And it only has Text property.

"Edward Yang" <neo_in_matrix@> wrote in message news:<OU**************@TK2MSFTNGP09.phx.gbl>...
Hi,

Since you know that strings are immutable, you can't clear or modify them in
any way (in theory).

Why not use a char array instead to store your password chars? It is at your
own disposal to create the array and destroy it. A few chars won't take up
too much memory.

Edward

"cppdev" <cp*****@yahoo.com> wrote in message
news:fc*************************@posting.google.co m...
Hi All!

I want to clear the string contents from sensitive information
such as passwords, and etc.

It's always a case that password will appear as string at some point
or another. And i feel uneasy leaving it hanging in memory indefinitely
(especially in case when string is Interned).

So at leats for the case when string is not interned i propose:

string pass = Console.ReadLine();
if (string.IsInterned(pass) == null)
{
unsafe
{
fixed(void* pv = pass)
{
char* pb = (char*)pv;
for(int i =0; i<pass.Length; ++i)
pb[i] = '0';
}
}
}
Console.WriteLine(pass);

Note: explicit RuntimeHelpers.OffsetToStringData is not needed.

What do you all think about this?

Jul 19 '05 #3
Yes i can use GetWindowText myself, but i also use
PasswordDeriveBytes to derive keys for encryption
from user password and that only takes a string.

"JD" <No@Where.com> wrote in message news:<#i**************@TK2MSFTNGP09.phx.gbl>...
Could you create a password control that stores the text into a byte[]
instead of a string so that the pass never gets interned?

- J

"cppdev" <cp*****@yahoo.com> wrote in message
news:fc**************************@posting.google.c om...
Hi,

I would love to use byte[] or char[],
but it's not my choice. I'm using TextControl
to get information from the user in winform.
And it only has Text property.

"Edward Yang" <neo_in_matrix@> wrote in message

news:<OU**************@TK2MSFTNGP09.phx.gbl>...
Hi,

Since you know that strings are immutable, you can't clear or modify them in any way (in theory).

Why not use a char array instead to store your password chars? It is at your own disposal to create the array and destroy it. A few chars won't take up too much memory.

Edward

"cppdev" <cp*****@yahoo.com> wrote in message
news:fc*************************@posting.google.co m...
> Hi All!
>
> I want to clear the string contents from sensitive information
> such as passwords, and etc.
>
> It's always a case that password will appear as string at some point
> or another. And i feel uneasy leaving it hanging in memory indefinitely > (especially in case when string is Interned).
>
> So at leats for the case when string is not interned i propose:
>
> string pass = Console.ReadLine();
> if (string.IsInterned(pass) == null)
> {
> unsafe
> {
> fixed(void* pv = pass)
> {
> char* pb = (char*)pv;
> for(int i =0; i<pass.Length; ++i)
> pb[i] = '0';
> }
> }
> }
> Console.WriteLine(pass);
>
> Note: explicit RuntimeHelpers.OffsetToStringData is not needed.
>
> What do you all think about this?

Jul 19 '05 #4
If a common string is used over and over again, .NET
may "intern" it or make a single instance of it and
whenever you try to create a new instance of it, it'll
just return you the reference to the main, interned one.

I believe this happens during JIT. It recognizes common
strings and just makes one copy of them.

-c

"News VS.NET ( MS ILM )" <sq**********@hotmail.com> wrote in message
news:uL*************@TK2MSFTNGP10.phx.gbl...
Excuse my now knowing
What does interned mean here.??

"JD" <No@Where.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Could you create a password control that stores the text into a byte[] instead of a string so that the pass never gets interned?

- J

"cppdev" <cp*****@yahoo.com> wrote in message
news:fc**************************@posting.google.c om...
Hi,

I would love to use byte[] or char[],
but it's not my choice. I'm using TextControl
to get information from the user in winform.
And it only has Text property.

"Edward Yang" <neo_in_matrix@> wrote in message news:<OU**************@TK2MSFTNGP09.phx.gbl>...
> Hi,
>
> Since you know that strings are immutable, you can't clear or modify
them in
> any way (in theory).
>
> Why not use a char array instead to store your password chars?

It is at
your
> own disposal to create the array and destroy it. A few chars
won't take
up
> too much memory.
>
> Edward
>
> "cppdev" <cp*****@yahoo.com> wrote in message
> news:fc*************************@posting.google.co m...
> > Hi All!
> >
> > I want to clear the string contents from sensitive information
> > such as passwords, and etc.
> >
> > It's always a case that password will appear as string at some

point > > or another. And i feel uneasy leaving it hanging in memory

indefinitely
> > (especially in case when string is Interned).
> >
> > So at leats for the case when string is not interned i propose: > >
> > string pass = Console.ReadLine();
> > if (string.IsInterned(pass) == null)
> > {
> > unsafe
> > {
> > fixed(void* pv = pass)
> > {
> > char* pb = (char*)pv;
> > for(int i =0; i<pass.Length; ++i)
> > pb[i] = '0';
> > }
> > }
> > }
> > Console.WriteLine(pass);
> >
> > Note: explicit RuntimeHelpers.OffsetToStringData is not needed. > >
> > What do you all think about this?



Jul 19 '05 #5
Chad

Thank you.
"Chad Myers" <cm****@N0.SP.AM.austin.rr.com> wrote in message
news:uO**************@TK2MSFTNGP09.phx.gbl...
If a common string is used over and over again, .NET
may "intern" it or make a single instance of it and
whenever you try to create a new instance of it, it'll
just return you the reference to the main, interned one.

I believe this happens during JIT. It recognizes common
strings and just makes one copy of them.

-c

"News VS.NET ( MS ILM )" <sq**********@hotmail.com> wrote in message
news:uL*************@TK2MSFTNGP10.phx.gbl...
Excuse my now knowing
What does interned mean here.??

"JD" <No@Where.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Could you create a password control that stores the text into a byte[] instead of a string so that the pass never gets interned?

- J

"cppdev" <cp*****@yahoo.com> wrote in message
news:fc**************************@posting.google.c om...
> Hi,
>
> I would love to use byte[] or char[],
> but it's not my choice. I'm using TextControl
> to get information from the user in winform.
> And it only has Text property.
>
> "Edward Yang" <neo_in_matrix@> wrote in message
news:<OU**************@TK2MSFTNGP09.phx.gbl>...
> > Hi,
> >
> > Since you know that strings are immutable, you can't clear or modify them in
> > any way (in theory).
> >
> > Why not use a char array instead to store your password chars? It is
at
your
> > own disposal to create the array and destroy it. A few chars

won't
take
up
> > too much memory.
> >
> > Edward
> >
> > "cppdev" <cp*****@yahoo.com> wrote in message
> > news:fc*************************@posting.google.co m...
> > > Hi All!
> > >
> > > I want to clear the string contents from sensitive information
> > > such as passwords, and etc.
> > >
> > > It's always a case that password will appear as string at some

point > > > or another. And i feel uneasy leaving it hanging in memory
indefinitely
> > > (especially in case when string is Interned).
> > >
> > > So at leats for the case when string is not interned i propose: > > >
> > > string pass = Console.ReadLine();
> > > if (string.IsInterned(pass) == null)
> > > {
> > > unsafe
> > > {
> > > fixed(void* pv = pass)
> > > {
> > > char* pb = (char*)pv;
> > > for(int i =0; i<pass.Length; ++i)
> > > pb[i] = '0';
> > > }
> > > }
> > > }
> > > Console.WriteLine(pass);
> > >
> > > Note: explicit RuntimeHelpers.OffsetToStringData is not needed. > > >
> > > What do you all think about this?



Jul 19 '05 #6
Chad Myers <cm****@N0.SP.AM.austin.rr.com> wrote:
If a common string is used over and over again, .NET
may "intern" it or make a single instance of it and
whenever you try to create a new instance of it, it'll
just return you the reference to the main, interned one.

I believe this happens during JIT. It recognizes common
strings and just makes one copy of them.


Fortunately it's not nearly as heuristic as that. All string
literals/constants are interned, and any string which you call Intern
on is interned. I don't believe anything else will get interned. You
also don't get the interned copy whenever you create a string with
identical contents - it's only if you're using the same string literal
or if you specifically ask for the interned version.

If the JIT started interning other strings, you'd end up with a
possible memory leak.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Jul 19 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Tino Lange | last post by:
Hi! I identified a bottleneck in my programs. I just want to "encrypt" data by easy xoring. Ok - that's no encryption at all - I know. But it's hardly readable - and that's enough :-) Just...
10
by: cppdev | last post by:
Hi All! I want to clear the string contents from sensitive information such as passwords, and etc. It's always a case that password will appear as string at some point or another. And i feel...
6
by: Marty | last post by:
Hi, I would like to replace "\r\n" by "_" within a specific string. I tried : strMyString.Replace('\r', '_'); strMyString.Replace('\n', '_'); or...
26
by: Hardy Wang | last post by:
Hi all, I know it is better to handle large string with a StringBuilder, but how does StringBuilder class improve the performance in the background? Thanks! -- WWW:...
10
by: Lonifasiko | last post by:
Hi, Just want to replace character at index 1 of a string with another character. Just want to replace character at that position. I thought Replace method would be overloaded with an index...
5
by: cameljs18 | last post by:
Converting a string variable into a string literal. How do I add the @ character in front of the string? I cannot add it when the string is created as it will affect other parts of the program. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
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,...
0
jinu1996
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 using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.