求助,帮忙把下面的Java代码翻译成IOS代码

回回回 发布于 2012/07/31 10:22
阅读 822
收藏 2
public static String generateSign(String data, String algorithm, String key) {
		if ("".equals(data) || null == data) {
			Log.e("data is null", "data is null");
			return "";
		}
		if ("".equals(algorithm) || null == algorithm) {
			Log.e("ArgumentNullException", "algorithm is null");
			return "";
		}
		if ("".equals(key) || null == key) {
			Log.e("key is null", "key is null");
			return "";
		}
		byte[] hash = null;
		data = data + key;
		try {
			data = Base64Coder.encodeString(new String(data.getBytes("UTF-8")));
		} catch (UnsupportedEncodingException e) {
			Log.e("UnsupportedEncodingException", e.getMessage());
		}
		try {
			MessageDigest messageDigest = null;
			if ("MD5".equals(algorithm)) {
				messageDigest = MessageDigest.getInstance("MD5");
			} else if ("SHA-256".equals(algorithm)) {
				messageDigest = MessageDigest.getInstance("SHA-256");
			} else if ("SHA-512".equals(algorithm)) {
				messageDigest = MessageDigest.getInstance("SHA-512");
			}
			messageDigest.reset();
			hash = messageDigest.digest(data.getBytes("UTF-8"));
		} catch (NoSuchAlgorithmException e) {
			Log.e("NoSuchAlgorithmException", e.getMessage());
		} catch (UnsupportedEncodingException e) {
			Log.e("UnsupportedEncodingException", e.getMessage());
		}
		return new String(Base64Coder.encode(hash));
	}

如题,寻求大牛帮我把上面的代码翻译成IOS的代码。非常感谢

以下是问题补充:

@回回回:我在IOS端写成这样的代码,但是两个的结果不一样 /** 生成签名 */ + (NSString *) generateSign:(NSString *)value andAlgorithm:(NSString *)algorithm andKey:(NSString *)key { NSData *data = [[NSString stringWithFormat:@"%@%@",value,key] dataUsingEncoding:NSUTF8StringEncoding]; NSString *base64Str = [data base64Encoding]; NSString *md5Str = [self encode:base64Str]; NSString *strResult = [[md5Str dataUsingEncoding:NSUTF8StringEncoding] base64Encoding]; return strResult; } (2012/07/31 10:39)
加载中
0
♬Windchaser
♬Windchaser
-(NSString *) generateSign:(NSString *)data algorithm:(NSString *)algorithm key:(NSString *)key{
    if ([data length] == 0 || [data isEqualToString:@""]) {
        NSLog(@"data is null");
        return nil;
    }
    
    Byte *hash = nil;
    data = [data stringByAppendingString:key];
    
    @try  {
        data = [[[NSString alloc] initWithData:[Base64 decode:data encoding:NSUTF8StringEncoding]];
    }
    @catch (NSException *exception) {
        NSLog(@"UnsupportedEncodingException %@:",getMessage)
    }
    
    return nil;
}

自己意会调试吧.

回回回
回回回
我写的方法在下面
回回回
回回回
非常感谢,最后我用GTMBase64+MD5解决了。
0
情天大圣
回回回
回回回
2什么呀。 实在是搞不出来,才请教别人的。
0
52java
52java
你自己弄弄吧,从功能来看,不是很难,至于细节你看看ios程序编程规范吧,帮你顶一下(ps:我不懂ios)
0
铂金小鬼
铂金小鬼
用xmlvm慢慢折腾吧!
0
回回回
回回回
/**
  生成签名
 */
+ (NSString *) generateSign:(NSString *)value andAlgorithm:(NSString *)algorithm andKey:(NSString *)key
{
    NSString *strResult = nil;
    NSData *data = [[NSString stringWithFormat:@"%@%@",value,key] dataUsingEncoding:NSUTF8StringEncoding];
    NSString *temp = [[NSString alloc] initWithBytes:[data bytes] length:[data length]  encoding:NSUTF8StringEncoding];
    data = [temp dataUsingEncoding:NSUTF8StringEncoding];
    data = [GTMBase64 encodeData:data];
    [temp release];

    NSString *base64Str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    const char *cStr = [base64Str UTF8String];
    unsigned char digest[16];
    CC_MD5(cStr, strlen(cStr), digest );
    [base64Str release];
    
    NSData *base64Data = [GTMBase64 encodeBytes:digest length:16];
    strResult = [[[NSString alloc] initWithData:base64Data encoding:NSUTF8StringEncoding] autorelease];
    return strResult;
}

这是我自己写的,其实我这个,也就是一个MD5+Base64的加密方法。  不过这个方法里面是Base64-->MD5-->Base64---->NSString
返回顶部
顶部