0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【iOS】Objective-Cでハッシュ値を計算するアプリを作ってみた(SHA256)

Last updated at Posted at 2020-12-19

雰囲気

なんか、

こんな感じで動く。

環境

Mac mini (M1チップセット Big Sur)
Xcode 12.2
(My Macで動作確認)

ViewController.m


//
//  ViewController.m
//  sha256
//
//  Created by hayao on 2020/12/16.
//

# import "ViewController.h"
# import <CommonCrypto/CommonHMAC.h>

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (NSString*)sha256:(NSString *)text{
    //NSLog(@"%@", text);
    const char *s = (char *)[text UTF8String];
    NSData *keyData=[NSData dataWithBytes:s length:strlen(s)];
     
    uint8_t digest[CC_SHA256_DIGEST_LENGTH]={0};
    CC_SHA256(keyData.bytes, keyData.length, digest);
    NSData *out = [NSData dataWithBytes:digest length:CC_SHA256_DIGEST_LENGTH];
    NSString *hash = [self hexadecimalStringFromData:out];
    return hash;
}

- (NSString *)hexadecimalStringFromData:(NSData *)data
{
    NSUInteger dataLength = data.length;
    if (dataLength == 0) {
        return nil;
    }

    const unsigned char *dataBuffer = data.bytes;
    NSMutableString *hexString  = [NSMutableString stringWithCapacity:(dataLength * 2)];
    for (int i = 0; i < dataLength; ++i) {
        [hexString appendFormat:@"%02x", dataBuffer[i]];
    }
    return [hexString copy];
}

- (IBAction)CalcHashButton:(id)sender {
    if ([_tv.text  isEqual: @""]) return;
    _hashedValue.text = [self sha256:_tv.text];
    [[UIPasteboard generalPasteboard] setValue:_hashedValue.text forPasteboardType:@"public.utf8-plain-text"];
    //NSLog(@"%@", _hashedValue.text);
    _message.text = @"copied to the clipboard";
}
@end

素晴らしすぎるコピペ元様(順不同、多謝)

Objective-Cでハッシュ関数を使う(SHA256)- Sim@Memo

(いまさら)Objective-C 型変換まとめ - Qiita

iOS13におけるプッシュ通知に必要なデバイストークンの取得方法 - Takahiro Octopress Blog

[Xcode]UIViewが画面の大きさによってずれてしまう時の対処法 | Code School

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?