Skip to content
This repository was archived by the owner on Jul 19, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions common/src/main/java/com/microsoft/alm/helpers/StringHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
public class StringHelper {
private static final Charset UTF8 = Charset.forName("UTF-8");

private static final Charset UTF16LE = Charset.forName("UTF-16LE");

public static final String Empty = "";

public static boolean endsWithIgnoreCase(final String haystack, final String needle) {
Expand Down Expand Up @@ -193,6 +195,17 @@ public static byte[] UTF8GetBytes(final String value) {
return result;
}

/**
* Encodes all the characters in the specified string into a sequence of UTF-16LE bytes.
*
* @param value The string containing the characters to encode.
* @return A byte array containing the results of encoding the specified set of characters.
*/
public static byte[] UTF16LEGetBytes(final String value) {
final byte[] result = value.getBytes(UTF16LE);
return result;
}

/**
* Decodes all the bytes in the specified byte array into a string.
*
Expand All @@ -204,6 +217,17 @@ public static String UTF8GetString(final byte[] bytes) {
return result;
}

/**
* Decodes all the bytes in the specified byte array into a string.
*
* @param bytes The byte array containing the sequence of bytes to decode.
* @return A string that contains the results of decoding the specified sequence of bytes.
*/
public static String UTF16LEGetString(final byte[] bytes) {
final String result = new String(bytes, UTF16LE);
return result;
}

/**
* Decodes a range of bytes from a byte array into a string.
*
Expand All @@ -216,4 +240,17 @@ public static String UTF8GetString(final byte[] bytes, final int index, final in
final String result = new String(bytes, index, count, UTF8);
return result;
}

/**
* Decodes a range of bytes from a byte array into a string.
*
* @param bytes The byte array containing the sequence of bytes to decode.
* @param index The index of the first byte to decode.
* @param count The number of bytes to decode.
* @return A string that contains the results of decoding the specified sequence of bytes.
*/
public static String UTF16LEGetString(final byte[] bytes, final int index, final int count) {
final String result = new String(bytes, index, count, UTF16LE);
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public E get(String key) {
final CredAdvapi32.CREDENTIAL credential = new CredAdvapi32.CREDENTIAL(pcredential.credential);

byte[] secretBytes = credential.CredentialBlob.getByteArray(0, credential.CredentialBlobSize);
final String secret = StringHelper.UTF8GetString(secretBytes);
final String secret = StringHelper.UTF16LEGetString(secretBytes);
final String username = credential.UserName;

cred = create(username, secret);
Expand Down Expand Up @@ -159,7 +159,7 @@ public boolean add(String key, E secret) {

final String username = getUsername(secret);
final String credentialBlob = getCredentialBlob(secret);
byte[] credBlob = StringHelper.UTF8GetBytes(credentialBlob);
byte[] credBlob = StringHelper.UTF16LEGetBytes(credentialBlob);

final CredAdvapi32.CREDENTIAL cred = buildCred(key, username, credBlob);

Expand Down