Change order of characters in a string in powershell - powershell-4.0

I have a thumbprint of a certificate stored in a variable and I need to change the order of its characters to write it to a registry value. If the thumbprint is like 1234567890, I need to change order of characters to 9078563412 - basically split the string to couples and write the couples in the reversed order. I don't know, how long the string will be, so I can't work with static positions of characters.
First problem is that when I tried $Thumbprint.ToString().length the result was 0, although it should've been 38. I tried to convert the Thumbprint variable to a new variable of the string type. When I used $ThumbString = $Thumbprint.ToString(), the $ThumbString variable is empty. I guess I will need to know the length for the conversion, even though I'm not sure of that. I also didn't find a way to split a string without using a delimiter.

I found a solution in one script on the net. This command does that:
$certSerialReversed = ""
-1..-38 |% {$certSerialReversed += $certSerial[2*$_] + $certSerial[2*$_ + 1]}
I found that it doesn't really matter how high the last number is (whether it is -1..-10 or -1..-50), so I just set the longest length a thumbprint can have.

Related

Abbreviating a UUID

What would be a good way to abbreviate UUID for use in a button in a user interface when the id is all we know about the target?
GitHub seems to abbreviate commit ids by taking 7 characters from the beginning. For example b1310ce6bc3cc932ce5cdbe552712b5a3bdcb9e5 would appear in a button as b1310ce. While not perfect this shorter version is sufficient to look unique in the context where it is displayed. I'm looking for a similar solution that would work for UUIDs. I'm wondering is some part of the UUID is more random than another.
The most straight forward option would be splitting at dash and using the first part. The UUID 42e9992a-8324-471d-b7f3-109f6c7df99d would then be abbreviated as 42e9992a. All of the solutions I can come up with seem equally arbitrary. Perhaps there is some outside the box user interface design solution that I didn't think of.
Entropy of a UUID is highest in the first bits for UUID V1 and V2, and evenly distributed for V3, V4 and V5. So, the first N characters are no worse than any other N characters subset.
For N=8, i.e. the group before the first dash, the odds of there being a collision within a list you could reasonably display within a single GUI screen is vanishingly small.
The question is whether you want to show part of the UUID or only ensure that unique strings are presented as shorter unique strings. If you want to focus on the latter, which appears to be the goal you are suggesting in your opening paragraph:
(...) While not perfect this shorter version is sufficient to look unique in
the context where it is displayed. (...)
you can make use of hashing.
Hashing:
Hashing is the transformation of a string of characters into a usually
shorter fixed-length value or key that represents the original string.
Hashing is used to index and retrieve items in a database because it
is faster to find the item using the shorter hashed key than to find
it using the original value.
Hashing is very common and easy to use across many of popular languages; simple approach in Python:
import hashlib
import uuid
encoded_str = uuid.UUID('42e9992a-8324-471d-b7f3-109f6c7df99d').bytes
hash_uuid = hashlib.sha1(encoded_str).hexdigest()
hash_uuid[:10]
'b6e2a1c885'
Expectedly, a small change in string will result in a different string correctly showing uniqueness.
# Second digit is replaced with 3, rest of the string remains untouched
encoded_str_two = uuid.UUID('43e9992a-8324-471d-b7f3-109f6c7df99d').bytes
hash_uuid_two = hashlib.sha1(encoded_str_two).hexdigest()
hash_uuid_two[:10]
'406ec3f5ae'
After thinking about this for a while I realised that the short git commit hash is used as part of command line commands. Since this requirement does not exist for UUIDs and graphical user interfaces I simply decided to use ellipsis for the abbreviation. Like so 42e9992...

Is there any way to read/modify the binary value of REG_SZ Registry Key using vbscript

Is there any way to read/modify the binary value of REG_SZ Registry Key using VBScript?
I just want to change the binary value of following REG_SZ Registry Key
HKCU\Control Panel\International\sPositiveSign
Note: I am asking for code to modify binary value (right click on key and select Modify Binary Value) and not the normal value of REG_SZ Binary Key. Please take a look at the screenshot in the below url:
[Here I dont want to edit "Select" text of aDefaultSelect Registry Key but I just want to remove two trailing zeroes of aDefaultSelect Registry Key]
http://www.imagesup.net/?di=1614190953031
Consider this code from Set registry key for wallpaper solid color. Here, the (string) value set is "0 0 0" in the following statement:
wshShell.RegWrite "HKEY_CURRENT_USER\Control Panel\Colors\Background", "0 0 0","REG_SZ"
Nothing stops you from passing any other valid string value to RegWrite, like in here:
wshShell.RegWrite "HKEY_CURRENT_USER\Control Panel\Colors\Background", Chr(1),"REG_SZ"
If that is what you mean by "binary data", that´s how to do it.
The most important thing to understand for this problem is that the "Binary Data" of this String is not some different data. It is a different representation of the same data. You see the bits directly (represented in Little Endian Hex form) instead of their interpretation as a Unicode String.
Each change in one format has an effect on the other format, you cannot just change a value in one.
So if you want to change the binary value, the easiest way is just to check what the new String representation looks like and input the new String. This will also change the binary data accordingly.
One thing to keep in mind here is that you have to use the SetStringValue method of StdRegProv not the RegWrite method.
The reason for this is that REG_SZ supports Unicode characters, and therefore each character takes up 16Bits in the Registry (i.e. Four digits in HEX representation). Most values on a western system are representable in ANSI, so for each character only two of those four digits are needed. This is the reason why there are two zeroes between every two digits in your example screenshot.
If you use RegWrite you can not write Unicode Strings so you can never write values to the positions where those zeroes are.
With SetStringValue you can write Unicode strings and therefore access all positions of the binary data.
Now if for some reason you would have to directly change values just based on their binary value, you could access them like this:
Const HKEY_CURRENT_USER = &H80000001
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
strKeyPath = "Software\Adobe\Acrobat Reader\11.0\Selection"
oReg.GetStringValue HKEY_CURRENT_USER, strKeyPath, "aDefaultSelect", bKey
For i = 1 to Len(bKey)
bString = bString & Hex(AscW(Mid(bKey,i,1))) & " "
Next
WScript.Echo bString
This gets you the exactly same values you see in the "Modify Binary Data" window.
The difference is that in the registry the format is Little Endian, so basically the order of each pair of numbers is exchanged. The second thing is that as I already noted if a number is smaller than four digits, it is still represented with four digits, so leading zeroes are added for the presentation in the registry.
Once you got this representation you can add to the binary data as you wish and then transform it back using a code like this:
<for each character in your new data>
bString = bString & ChrW( <modified binary value of a single character>)
<next>
oReg.SetStringValue HKEY_CURRENT_USER, strKeyPath, "aDefaultSelect", bString
Now as for your wish to remove the "trailing zeroes". First you have to understand that due to the Little Endian concept depending on the string this might be actually the beginning of a character not its end. Second, each character is always 16Bit. You can not just remove some of the bits as they are just fillers to pad the characters. If you do not pay respect to those rules the outcome on the final string may be very different from what you expected.
I would therefore highly recommend that you manually do your changes (in the binary data window if you want) then look at what it does to the string, save the resulting string, and just write exactly that string back using SetStringValue.

Julia: Strange characters in my string

I scraped some text from the internet, which I put in an UTF8String. I can use this string normally, but when I select some specific characters (strange character with accents, like in my case ú), which are not part of the UTF8 standard, I get an error, saying that I used invalid indexes. This only happens when the string contains strange characters; my code works with normal string that do not contain strange characters.
Any way to solve this?
EDIT:
I have a variable word of type SubString{UTF8String}
When I use do method(word), no problems occur. When I do method(word[2:end]) (assuming length of at least 2), I get an error in case the second character is strange (not in UTF8).
Julia does indexing on byte positions instead of character position. It is way more efficient for a variable length encoding like UTF-8, but it makes some operations use some more boilerplate.
The problem is that some codepoints is encoded as multiple bytes and when you slice the string from 2:end you would have got half of the first character (witch is invalid and you get an error).
The solution is to get the second valid index instead of 2 in the slice. I think that is something like str[nextind(str, 1):end]
PS. Sorry for a less than clear answer on my phone.
EDIT:
I tried this, and it seems like SubString{UTF8String} and UTF8String has different behaviour on slicing. I've reported it as bug #7811 on GitHub.

Jmeter : Removing Spaces using RegEx

Jmeter :
I am having a JSON from which I have to fetch value of "ci".
I am using the following RegEx : ci:\s*(.*?)\" and getting the following result RegEx tester:
Match count: 1
Match1[0]=ci: 434547"
Match1=434547
Issue is Match1[0] is having spaces because of which while running the load test it says
: Server Error - Could not convert JSON to Object
Need help is correcting this RegEx.
Basically, your RegEx is fine. This is the way I would look for it too, the first group (Match[1]) would give you 434613, which is the value you are looking for. As I don't know that piece of software you are using, I have no idea why using just that match doesn't work.
Here is an idea to work around that: if the value will always be the only numeric value in the string, you could simplify the RegEx to:
\d+
This will give you a numeric value that is at least 1 digit long. If there are other numeric values in the string though, but these have different lengths, try this:
\d{m,n} --> between m and n digits long
\d{n,} --> at least n digits long
\d{0,n} --> not more than n digits long
This is not as secure / reliable as the original RegEx (since it assumes some certain conditions), but it might work in your case, because you don't have to look for groups but just use the whole matched text. Tell me if it helped!

What does this VB6 variable declaration do?

I just found this in some old code, and I'm not sure what it means.
Dim sTemp As String * 1
What is the * 1 at the end?
Thanks!
It means that the variable is a string of exactly one character in length.
Essentially, a fixed-length string.
It's a fixed length string of one character. This was handy cause you could define a structure of fixed strings and read a file right into the structure.
It creates a fixed-length string. In that example, the variable will only ever contain one character.

Resources