Commit 71e9630b authored by admin's avatar admin

完成登陆接口 添加MD5 base64加密方法的工具类

parent 7b25932f
......@@ -16,6 +16,7 @@
841BBCDE1BE49E18007A21BF /* RankWorkDetailCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 841BBCDD1BE49E18007A21BF /* RankWorkDetailCell.m */; };
8442BA5C1BDB8FDB005E5657 /* MineTableHeaderView.m in Sources */ = {isa = PBXBuildFile; fileRef = 8442BA5B1BDB8FDB005E5657 /* MineTableHeaderView.m */; };
8474A01C1BE368D700315F30 /* HomeCellItem.m in Sources */ = {isa = PBXBuildFile; fileRef = 8474A01B1BE368D700315F30 /* HomeCellItem.m */; };
8474C5831BE751A2007DCF19 /* CommonFunc.m in Sources */ = {isa = PBXBuildFile; fileRef = 8474C5821BE751A2007DCF19 /* CommonFunc.m */; };
84846E971BE069BC0010550A /* DateButton.m in Sources */ = {isa = PBXBuildFile; fileRef = 84846E961BE069BC0010550A /* DateButton.m */; };
84846E9A1BE07CDF0010550A /* RankingListCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 84846E991BE07CDF0010550A /* RankingListCell.m */; };
848699B01BDF810900859DFE /* InspectListCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 8486999F1BDF810900859DFE /* InspectListCell.m */; };
......@@ -87,6 +88,9 @@
8442BA5B1BDB8FDB005E5657 /* MineTableHeaderView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MineTableHeaderView.m; sourceTree = "<group>"; };
8474A01A1BE368D700315F30 /* HomeCellItem.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HomeCellItem.h; sourceTree = "<group>"; };
8474A01B1BE368D700315F30 /* HomeCellItem.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HomeCellItem.m; sourceTree = "<group>"; };
8474C5811BE751A2007DCF19 /* CommonFunc.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CommonFunc.h; sourceTree = "<group>"; };
8474C5821BE751A2007DCF19 /* CommonFunc.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CommonFunc.m; sourceTree = "<group>"; };
8474C5851BE77311007DCF19 /* Url.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Url.h; sourceTree = "<group>"; };
84846E951BE069BC0010550A /* DateButton.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DateButton.h; sourceTree = "<group>"; };
84846E961BE069BC0010550A /* DateButton.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DateButton.m; sourceTree = "<group>"; };
84846E981BE07CDF0010550A /* RankingListCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RankingListCell.h; sourceTree = "<group>"; };
......@@ -349,6 +353,15 @@
path = WordOfMouth;
sourceTree = "<group>";
};
8474C5801BE751A2007DCF19 /* CommonFunc */ = {
isa = PBXGroup;
children = (
8474C5811BE751A2007DCF19 /* CommonFunc.h */,
8474C5821BE751A2007DCF19 /* CommonFunc.m */,
);
path = CommonFunc;
sourceTree = "<group>";
};
8486999B1BDF810900859DFE /* Inspect */ = {
isa = PBXGroup;
children = (
......@@ -578,6 +591,7 @@
84970E261BD8DEFE00C1728A /* Tools */ = {
isa = PBXGroup;
children = (
8474C5801BE751A2007DCF19 /* CommonFunc */,
84F166981BE715D20061D350 /* ZanButton */,
84DB9BC11BDCD49900822CC4 /* CustomDropMenu */,
);
......@@ -622,6 +636,7 @@
isa = PBXGroup;
children = (
84970E3F1BD9127A00C1728A /* Constant.h */,
8474C5851BE77311007DCF19 /* Url.h */,
);
path = Macro;
sourceTree = "<group>";
......@@ -991,6 +1006,7 @@
84970E281BD8DEFE00C1728A /* AppDelegate.m in Sources */,
84AB20A51BE648EC00B66F13 /* PictureViewController.m in Sources */,
841BBCDE1BE49E18007A21BF /* RankWorkDetailCell.m in Sources */,
8474C5831BE751A2007DCF19 /* CommonFunc.m in Sources */,
848699B31BDF810900859DFE /* InspectTaskViewController.m in Sources */,
848699B11BDF810900859DFE /* MenuButton.m in Sources */,
840ABECF1BE20A53006E49C5 /* RankShopDetialCell.m in Sources */,
......
//
// Url.h
// redstar
//
// Created by admin on 15/11/2.
// Copyright © 2015年 ZWF. All rights reserved.
//
#ifndef Url_h
#define Url_h
// url
#define kRedStarURL @"http://218.244.151.129:7580/"
// login
#define kLoginURL @"redstar-server/rest/user/login/"
#endif /* Url_h */
......@@ -15,6 +15,9 @@
#import "ChartViewController.h"
#import "MineViewController.h"
#import <AFNetworking.h>
#import "CommonFunc.h"
#define kUsernameTableViewCell @"usernameTableViewCell"
@interface LoginViewController () <CustomDropMenuDelegate, UITextFieldDelegate, UITableViewDelegate, UITableViewDataSource, UITabBarControllerDelegate> {
......@@ -41,7 +44,7 @@
self.loginView.usernameTextFiled.delegate = self;
// 初始化数组
self.allUserDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"admin1", @"admin", @"test", @"test11",@"hello22" , @"hello", nil];
self.allUserDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"thoradmin", @"admin", nil];
}
- (void)loadView
......@@ -70,26 +73,48 @@
*/
- (void)loginButtonClick:(UIButton *)sender
{
// 跳转到tabbarController
AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
session.requestSerializer = [AFJSONRequestSerializer serializer];
[session.requestSerializer setValue:@"application/json;charset=utf-8" forHTTPHeaderField:@"Content-Type"];
NSString *urlStr = [NSString stringWithFormat:@"%@%@%@", kRedStarURL, kLoginURL, self.loginView.usernameTextFiled.text];
NSString *password = self.loginView.passwordTextFiled.text;
NSString *passwordMD5 = [CommonFunc md5:password];
NSDictionary *parameters = @{@"authenticode":@"211534962",@"password":passwordMD5};
[session POST:urlStr parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {
if ([responseObject[@"message"] isEqualToString:@"ok"]) {
[self setupTabbar];
} else {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:[NSString stringWithFormat:@"%@", responseObject[@"message"]] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[alertView show];
}
} failure:^(NSURLSessionDataTask *task, NSError *error) {
NSLog(@"error = %@", error);
}];
}
//
- (void)setupTabbar
{
// 跳转到tabbarController
UITabBarController *tabBC = [[UITabBarController alloc] init];
tabBC.delegate = self;
HomeViewController *home=[[HomeViewController alloc]init];
UINavigationController *homeNav = [self setUpOneChildViewController:home title:@"首页" imageName:@"home_unchecked" selImageName:@"home_checked"];
AnnounceViewController *announce = [[AnnounceViewController alloc] init];
UINavigationController *announceNav = [self setUpOneChildViewController:announce title:@"公告" imageName:@"notice_unchecked" selImageName:@"notice_checked"];
ChartViewController *chart = [[ChartViewController alloc] init];
UINavigationController *chartNav = [self setUpOneChildViewController:chart title:@"报表查询" imageName:@"report_unchecked" selImageName:@"report_checked"];
MineViewController *mine = [[MineViewController alloc] init];
mine.tabBarItem.title= @"我的";
mine.tabBarItem.image = [UIImage imageNamed:@"aboutme_unchecked"];
mine.tabBarItem.selectedImage = [[UIImage imageNamed:@"aboutme_checke"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
kNavigationBarColor, NSForegroundColorAttributeName,
......@@ -97,7 +122,6 @@
tabBC.viewControllers = @[homeNav, announceNav, chartNav, mine];
[self presentViewController:tabBC animated:YES completion:nil];
}
// 找回密码
......
//
// CommonFunc.h
// login
//
// Created by admin on 15/11/2.
// Copyright © 2015年 ZWF. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <CommonCrypto/CommonDigest.h>
#define __BASE64( text ) [CommonFunc base64StringFromText:text]
#define __TEXT( base64 ) [CommonFunc textFromBase64String:base64]
@interface CommonFunc : NSObject
/******************************************************************************
函数名称 : + (NSString *)base64StringFromText:(NSString *)text
函数描述 : 将文本转换为base64格式字符串
输入参数 : (NSString *)text 文本
输出参数 : N/A
返回参数 : (NSString *) base64格式字符串
备注信息 :
******************************************************************************/
+ (NSString *)base64StringFromText:(NSString *)text;
/******************************************************************************
函数名称 : + (NSString *)textFromBase64String:(NSString *)base64
函数描述 : 将base64格式字符串转换为文本
输入参数 : (NSString *)base64 base64格式字符串
输出参数 : N/A
返回参数 : (NSString *) 文本
备注信息 :
******************************************************************************/
+ (NSString *)textFromBase64String:(NSString *)base64;
+ (NSString *)md5:(NSString *)str;
@end
//
// CommonFunc.m
// login
//
// Created by admin on 15/11/2.
// Copyright © 2015年 ZWF. All rights reserved.
//
#import "CommonFunc.h"
//引入IOS自带密码库
#import <CommonCrypto/CommonCryptor.h>
//空字符串
#define LocalStr_None @""
static const char encodingTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
@implementation CommonFunc
+ (NSString *)base64StringFromText:(NSString *)text
{
if (text && ![text isEqualToString:LocalStr_None]) {
//取项目的bundleIdentifier作为KEY 改动了此处
//NSString *key = [[NSBundle mainBundle] bundleIdentifier];
NSData *data = [text dataUsingEncoding:NSUTF8StringEncoding];
//IOS 自带DES加密 Begin 改动了此处
//data = [self DESEncrypt:data WithKey:key];
//IOS 自带DES加密 End
return [self base64EncodedStringFrom:data];
}
else {
return LocalStr_None;
}
}
+ (NSString *)textFromBase64String:(NSString *)base64
{
if (base64 && ![base64 isEqualToString:LocalStr_None]) {
//取项目的bundleIdentifier作为KEY 改动了此处
//NSString *key = [[NSBundle mainBundle] bundleIdentifier];
NSData *data = [self dataWithBase64EncodedString:base64];
//IOS 自带DES解密 Begin 改动了此处
//data = [self DESDecrypt:data WithKey:key];
//IOS 自带DES加密 End
return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
}
else {
return LocalStr_None;
}
}
/******************************************************************************
函数名称 : + (NSData *)DESEncrypt:(NSData *)data WithKey:(NSString *)key
函数描述 : 文本数据进行DES加密
输入参数 : (NSData *)data
(NSString *)key
输出参数 : N/A
返回参数 : (NSData *)
备注信息 : 此函数不可用于过长文本
******************************************************************************/
+ (NSData *)DESEncrypt:(NSData *)data WithKey:(NSString *)key
{
char keyPtr[kCCKeySizeAES256+1];
bzero(keyPtr, sizeof(keyPtr));
[key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
NSUInteger dataLength = [data length];
size_t bufferSize = dataLength + kCCBlockSizeAES128;
void *buffer = malloc(bufferSize);
size_t numBytesEncrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmDES,
kCCOptionPKCS7Padding | kCCOptionECBMode,
keyPtr, kCCBlockSizeDES,
NULL,
[data bytes], dataLength,
buffer, bufferSize,
&numBytesEncrypted);
if (cryptStatus == kCCSuccess) {
return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
}
free(buffer);
return nil;
}
/******************************************************************************
函数名称 : + (NSData *)DESEncrypt:(NSData *)data WithKey:(NSString *)key
函数描述 : 文本数据进行DES解密
输入参数 : (NSData *)data
(NSString *)key
输出参数 : N/A
返回参数 : (NSData *)
备注信息 : 此函数不可用于过长文本
******************************************************************************/
+ (NSData *)DESDecrypt:(NSData *)data WithKey:(NSString *)key
{
char keyPtr[kCCKeySizeAES256+1];
bzero(keyPtr, sizeof(keyPtr));
[key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
NSUInteger dataLength = [data length];
size_t bufferSize = dataLength + kCCBlockSizeAES128;
void *buffer = malloc(bufferSize);
size_t numBytesDecrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmDES,
kCCOptionPKCS7Padding | kCCOptionECBMode,
keyPtr, kCCBlockSizeDES,
NULL,
[data bytes], dataLength,
buffer, bufferSize,
&numBytesDecrypted);
if (cryptStatus == kCCSuccess) {
return [NSData dataWithBytesNoCopy:buffer length:numBytesDecrypted];
}
free(buffer);
return nil;
}
/******************************************************************************
函数名称 : + (NSData *)dataWithBase64EncodedString:(NSString *)string
函数描述 : base64格式字符串转换为文本数据
输入参数 : (NSString *)string
输出参数 : N/A
返回参数 : (NSData *)
备注信息 :
******************************************************************************/
+ (NSData *)dataWithBase64EncodedString:(NSString *)string
{
if (string == nil)
[NSException raise:NSInvalidArgumentException format:nil];
if ([string length] == 0)
return [NSData data];
static char *decodingTable = NULL;
if (decodingTable == NULL)
{
decodingTable = malloc(256);
if (decodingTable == NULL)
return nil;
memset(decodingTable, CHAR_MAX, 256);
NSUInteger i;
for (i = 0; i < 64; i++)
decodingTable[(short)encodingTable[i]] = i;
}
const char *characters = [string cStringUsingEncoding:NSASCIIStringEncoding];
if (characters == NULL) // Not an ASCII string!
return nil;
char *bytes = malloc((([string length] + 3) / 4) * 3);
if (bytes == NULL)
return nil;
NSUInteger length = 0;
NSUInteger i = 0;
while (YES)
{
char buffer[4];
short bufferLength;
for (bufferLength = 0; bufferLength < 4; i++)
{
if (characters[i] == '\0')
break;
if (isspace(characters[i]) || characters[i] == '=')
continue;
buffer[bufferLength] = decodingTable[(short)characters[i]];
if (buffer[bufferLength++] == CHAR_MAX) // Illegal character!
{
free(bytes);
return nil;
}
}
if (bufferLength == 0)
break;
if (bufferLength == 1) // At least two characters are needed to produce one byte!
{
free(bytes);
return nil;
}
// Decode the characters in the buffer to bytes.
bytes[length++] = (buffer[0] << 2) | (buffer[1] >> 4);
if (bufferLength > 2)
bytes[length++] = (buffer[1] << 4) | (buffer[2] >> 2);
if (bufferLength > 3)
bytes[length++] = (buffer[2] << 6) | buffer[3];
}
bytes = realloc(bytes, length);
return [NSData dataWithBytesNoCopy:bytes length:length];
}
/******************************************************************************
函数名称 : + (NSString *)base64EncodedStringFrom:(NSData *)data
函数描述 : 文本数据转换为base64格式字符串
输入参数 : (NSData *)data
输出参数 : N/A
返回参数 : (NSString *)
备注信息 :
******************************************************************************/
+ (NSString *)base64EncodedStringFrom:(NSData *)data
{
if ([data length] == 0)
return @"";
char *characters = malloc((([data length] + 2) / 3) * 4);
if (characters == NULL)
return nil;
NSUInteger length = 0;
NSUInteger i = 0;
while (i < [data length])
{
char buffer[3] = {0,0,0};
short bufferLength = 0;
while (bufferLength < 3 && i < [data length])
buffer[bufferLength++] = ((char *)[data bytes])[i++];
// Encode the bytes in the buffer to four characters, including padding "=" characters if necessary.
characters[length++] = encodingTable[(buffer[0] & 0xFC) >> 2];
characters[length++] = encodingTable[((buffer[0] & 0x03) << 4) | ((buffer[1] & 0xF0) >> 4)];
if (bufferLength > 1)
characters[length++] = encodingTable[((buffer[1] & 0x0F) << 2) | ((buffer[2] & 0xC0) >> 6)];
else characters[length++] = '=';
if (bufferLength > 2)
characters[length++] = encodingTable[buffer[2] & 0x3F];
else characters[length++] = '=';
}
return [[NSString alloc] initWithBytesNoCopy:characters length:length encoding:NSASCIIStringEncoding freeWhenDone:YES];
}
// md5encode
+(NSString*) md5:(NSString*) str
{
const char *cStr = [str UTF8String];
unsigned char result[CC_MD5_DIGEST_LENGTH];
CC_MD5( cStr, (unsigned int)strlen(cStr), result );
NSMutableString *hash = [NSMutableString string];
for(int i=0;i<CC_MD5_DIGEST_LENGTH;i++) {
[hash appendFormat:@"%02X",result[i]];
}
return [hash lowercaseString];
}
@end
......@@ -41,5 +41,13 @@
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
</dict>
</plist>
......@@ -13,5 +13,7 @@
// You will also need to set the Prefix Header build setting of one or more of your targets to reference this file.
#import "Constant.h"
#import "Url.h"
#endif /* redstar_pch */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment