//
//  AESTools.m
//  Showroom
//
//  Created by RD on 2021/3/8.
//

#import "AESTools.h"
#import "AESEncrypt.h"
#import "NSString+safe.h"
#import "NSString+convert.h"

typedef NS_ENUM(NSUInteger, AESType) {
    AES_ECB = 0,
    AES_CBC  = 1
};

typedef NS_ENUM(NSUInteger, AESNum) {
    AES_128 = 0,
    AES_256  = 1
};

@interface AESTools ()

@property (nonatomic, copy) NSString *AESKey;     // 确定使用的密钥
@property (nonatomic, copy) NSString *defaultKey; // 固定秘钥
@property (nonatomic, copy) NSString *randomKey;  // 随机密钥
@property (nonatomic, copy) NSString *iv;         // 向量 偏移量

@property (nonatomic, assign) AESType aesType;
@property (nonatomic, assign) AESNum aesNum;

@end
@implementation AESTools

DEF_SINGLETON(AESTools)

- (instancetype)init{
    self = [super init];
    if (self) {
        _defaultKey = @"31323334353631323334353637383930";
        _iv = @"22222222222222222222222222222222";
        
        //_randomKey = [self createRandomKey];
    }
    return self;
}


- (NSString *)encrypt:(NSString *)content{
    
    NSString *result = @"";
    if (_aesType == AES_ECB) {
        result = [self ecbEncrypt:content];
    }else{
        result = [self cbcEncrypt:content];
    }
    return result;
}

- (NSString *)decrypt:(NSString *)content{
    
    NSString *result = @"";
    if (_aesType == AES_ECB) {
        result = [self ecbDecrypt:content];
    }else{
        result = [self cbcDecrypt:content];
    }
    return result;
}


// cbc加密
-(NSString *)cbcEncrypt:(NSString *)content{

    NSString *result = [AESEncrypt cbcEncrypt:content key:_AESKey];
    return result;
}

// ecb加密
-(NSString *)ecbEncrypt:(NSString *)content{
    
    NSString *result = [AESEncrypt ecbEncrypt:content key:_AESKey];
    return result;
}


// cbc解密
-(NSString *)cbcDecrypt:(NSString *)content{
    
    NSString *result = [AESEncrypt cbcDecrypt:content key:_AESKey];
    return result;
}

// ecb解密
-(NSString *)ecbDecrypt:(NSString *)content{
    
    NSString *result = [AESEncrypt ecbDecrypt:content key:_AESKey];
    return result;
}


#pragma mark - -

- (void)initFornoWorld{
    _AESKey = @"1234561234567890";
    _aesType = AES_ECB;
    _aesNum = AES_128;
}

- (void)setVersion:(NSString *)version{
    
    int v1 = [version rds_getSubStringSafetyWithRange:NSMakeRange(0, 4)].intValue;
    int v2 = [version rds_getSubStringSafetyWithRange:NSMakeRange(4, 2)].intValue;
    int v3 = [version rds_getSubStringSafetyWithRange:NSMakeRange(6, 2)].intValue;
    
    // 确定是用固定密钥还是随机密钥
    if (v1 == 1) {
        _AESKey = _defaultKey;
    }
    else if (v1 == 2){
        _AESKey = _randomKey;
    }
    else if (v1 == 3){
        // 未实现
    }
    
    // 1 ecb加密 2cbc加密
    if (v2 == 1) {
        _aesType = AES_ECB;
    }else if (v2 == 2){
        _aesType = AES_CBC;
    }
    
    
    if (v3 == 1) {
        // 128位
        _aesNum = AES_128;
    } else if (v3 == 2){
        // 256位
        _aesNum = AES_256;
        _AESKey = [_AESKey stringByAppendingString:_AESKey];
    }
    
}

// 加密后的随机密钥
- (NSString *)getRandomKeyString{
        
    NSString *result = @"";
    NSString *key = _defaultKey;
    
    if (_aesNum == AES_128) {
        
    }else{
        key = [key stringByAppendingString:key];
    }
    
    if (_aesType == AES_ECB) {
        result = [AESEncrypt ecbEncrypt:_randomKey key:key];
    }else{
        result = [AESEncrypt cbcEncrypt:_randomKey key:key];
    }
    
    return result;
}

/// 生成随机密钥
- (NSString *)createRandomKey{
    
    // 随机生成16字节字符串
    char data[32];
    for (int x=0;x<32;data[x++] = (char)('A' + (arc4random_uniform(26))));
    NSString *randomStr = [[NSString alloc] initWithBytes:data length:32 encoding:NSUTF8StringEncoding];
    NSLog(@"随机密钥:%@",randomStr);
    
    // 转16进制字符串
    //randomStr = [randomStr rds_stringTohexString];
    return randomStr;
}
@end
