J'ai une chaîneMD5 avec ASCII Char
wDevCopyright = [NSString stringWithFormat:@"Copyright: %c 1995 by WIRELESS.dev, Corp Communications Inc., All rights reserved.",0xa9];
et munge Je l'appelle
-(NSString *)getMD5:(NSString *)source
{
const char *src = [source UTF8String];
unsigned char result[CC_MD5_DIGEST_LENGTH];
CC_MD5(src, strlen(src), result);
return [NSString stringWithFormat:
@"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
result[0], result[1], result[2], result[3],
result[4], result[5], result[6], result[7],
result[8], result[9], result[10], result[11],
result[12], result[13], result[14], result[15]
]; //ret;
}
en raison de 0xA9 * src = [UTF8String source] ne crée pas char qui représente la chaîne , renvoyant ainsi un munge qui n'est pas comparable avec d'autres plates-formes.
J'ai essayé d'encoder le char avec NSASCIIStringEncoding mais il a cassé le code. Comment appeler CC_MD5 avec une chaîne qui a des caractères ASCII et obtient le même hachage que dans Java?
mise à jour à la demande de code:
Java
private static char[] kTestASCII = {
169
};
System.out.println("\n\n>>>>> msg## " + (char)0xa9 + " " + (char)169 + "\n md5 " + md5(new String(kTestASCII), false) //unicode = false
Résultat >>>>> msg ## \ 251 \ 251 md5 a252c2c85a9e7756d5ba5da9949d57ed
ObjC
char kTestASCII [] = {
169
};
NSString *testString = [NSString stringWithCString:kTestASCII encoding:NSUTF8StringEncoding];
NSLog(@">>>> objC msg## int %d char %c md5: %@", 0xa9, 169, [self getMD5:testString]);
Résultat >>>> objC msg ## int 169 char © md5: 9b759040321a408a5c7768b4511287a6
** Comme indiqué précédemment - sans le 0xa9 les hachages dans Java et ObjC sont les mêmes. Je suis en train d'obtenir le hachage pour 0xA9 même en Java et ObjC
code Java MD5
private static char[] kTestASCII = {
169
};
md5(new String(kTestASCII), false);
/**
* Compute the MD5 hash for the given String.
* @param s the string to add to the digest
* @param unicode true if the string is unciode, false for ascii strings
*/
public synchronized final String md5(String value, boolean unicode)
{
MD5();
MD5.update(value, unicode);
return WUtilities.toHex(MD5.finish());
}
public synchronized void update(String s, boolean unicode)
{
if (unicode)
{
char[] c = new char[s.length()];
s.getChars(0, c.length, c, 0);
update(c);
}
else
{
byte[] b = new byte[s.length()];
s.getBytes(0, b.length, b, 0);
update(b);
}
}
public synchronized void update(byte[] b)
{
update(b, 0, b.length);
}
//--------------------------------------------------------------------------------
/**
* Add a byte sub-array to the digest.
*/
public synchronized void update(byte[] b, int offset, int length)
{
for (int n = offset; n < offset + length; n++)
update(b[n]);
}
/**
* Add a byte to the digest.
*/
public synchronized void update(byte b)
{
int index = (int)((count >>> 3) & 0x03f);
count += 8;
buffer[index] = b;
if (index >= 63)
transform();
}
Je crois que mon problème est avec l'utilisation NSData withEncoding par opposition à un C char [] ou l'octet Java []. Alors, quelle est la meilleure façon de lancer mes propres octets dans un octet [] dans objC?
Pourriez-vous s'il vous plaît indiquer votre question? Nous aimons voir des questions explicites ici. – Oded
Merci de me guider pour devenir une meilleure liste de citoyens. –
À quoi ressemble votre code Java? –