λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°

🍎 iOS/iOS Application

[iOS, Swift, Objective-C] μ•”ν˜Έν™” - SHA256 κ΅¬ν˜„ν•˜κΈ°

πŸ’‘ SHA256μ΄λž€?

SHA-256은 SHA(Secure Hash Algorithm) μ•Œκ³ λ¦¬μ¦˜μ˜ ν•œ μ’…λ₯˜λ‘œμ„œ SHA-2 계열 쀑 ν•˜λ‚˜μ΄λ©°, 2^256만큼 경우의 수λ₯Ό λ§Œλ“€ 수 μžˆλ‹€.

μ–΄λ–€ 길이의 값을 μž…λ ₯ν•˜λ”λΌλ„ 256λΉ„νŠΈμ˜ κ³ μ •λœ κ²°κ³Όκ°’(64자리 λ¬Έμžμ—΄(32byte))을 λ°˜ν™˜ν•˜κ³ ,

SHA-256은 단방ν–₯ μ•”ν˜Έν™” 방식이기 λ•Œλ¬Έμ— λ³΅ν˜Έν™”κ°€ λΆˆκ°€λŠ₯ν•©λ‹ˆλ‹€.

 

SHA256 - ν•΄μ‹œλ„·

SHA-256은 SHA(Secure Hash Algorithm) μ•Œκ³ λ¦¬μ¦˜μ˜ ν•œ μ’…λ₯˜λ‘œμ„œ 256λΉ„νŠΈλ‘œ κ΅¬μ„±λ˜λ©° 64자리 λ¬Έμžμ—΄μ„ λ°˜ν™˜ν•œλ‹€. SHA-256은 미ꡭ의 κ΅­λ¦½ν‘œμ€€κΈ°μˆ μ—°κ΅¬μ†Œ(NIST; National Institute of Standards and Technology)에 μ˜ν•΄ κ³΅ν‘œλœ

wiki.hash.kr

 


πŸ’‘ ν•΄μ‹œ(Hash)λž€?

ν•΄μ‹œ(hash)λž€ 단방ν–₯ μ•”ν˜Έν™” κΈ°λ²•μœΌλ‘œ ν•΄μ‹œν•¨μˆ˜(ν•΄μ‹œ μ•Œκ³ λ¦¬μ¦˜)λ₯Ό μ΄μš©ν•˜μ—¬ κ³ μ •λœ 길이의 μ•”ν˜Έν™”λœ λ¬Έμžμ—΄λ‘œ λ°”κΏ”λ²„λ¦¬λŠ” 것을 의미

  • ν•΄μ‹œ ν•¨μˆ˜(hash function) : ν•΄μ‹œ μ•Œκ³ λ¦¬μ¦˜κ³Ό λ™μΌν•˜λ©° 큰 데이터 집합을 전체 집합을 λ‚˜νƒ€λ‚΄λŠ” μž‘μ€ μ§‘ν•©μœΌλ‘œ λ³€ν™˜ν•˜λŠ” μž‘μ—…(=ν•΄μ‹±)을 μˆ˜ν–‰.  μ΄ λ•Œ,  맀핑 μ „ μ›λž˜ λ°μ΄ν„°μ˜ 값을 ν‚€(key), 맀핑 ν›„ λ°μ΄ν„°μ˜ 값을 ν•΄μ‹œκ°’(hash value), λ§€ν•‘ν•˜λŠ” 과정을 ν•΄μ‹±(hashing)이라고 ν•œλ‹€.
  • ν•΄μ‹œκ°’(Hash Value) :  ν•¨μˆ˜μ— λŒ€ν•œ μ‘λ‹΅μœΌλ‘œ 제곡된 κ°’μœΌλ‘œ μ•Œλ €μ§„ 데이터

 

Swiftμ—μ„œ μ–΄λ–€ 데이터λ₯Ό SHA-255 ν•΄μ‹œκ°’μœΌλ‘œ μ „ν™˜ν•˜λ €λ©΄ cryptoKit λͺ¨λ“ˆμ˜ SHA256을 μ‚¬μš©ν•˜λ©΄ λœλ‹€.

sha256λŠ” SHA256.Digests인데, μ—¬κΈ°μ„œ DigestλŠ” hash ν•¨μˆ˜λ₯Ό ν†΅κ³Όν•œ μ΄ν›„μ˜ 데이터λ₯Ό μ˜λ―Έν•œλ‹€.

[Swift κ΅¬ν˜„]

import Foundation
import CommonCrypto

extension Data{
    public func sha256() -> String{
        return hexStringFromData(input: digest(input: self as NSData))
    }
    
    private func digest(input : NSData) -> NSData {
        let digestLength = Int(CC_SHA256_DIGEST_LENGTH)
        var hash = [UInt8](repeating: 0, count: digestLength)
        CC_SHA256(input.bytes, UInt32(input.length), &hash)
        return NSData(bytes: hash, length: digestLength)
    }
    
    //sha-256을 호좜, 리턴을 byte[]둜 λ°›κΈ° λ•Œλ¬Έμ—, String λ³€ν™˜μ΄ ν•„μš”
    private  func hexStringFromData(input: NSData) -> String {
        var bytes = [UInt8](repeating: 0, count: input.length)
        input.getBytes(&bytes, length: input.length)
        
        var hexString = ""
        for byte in bytes {
            hexString += String(format:"%02x", UInt8(byte))
        }
        
        return hexString
    }
}

public extension String {
    func sha256() -> String{
        if let stringData = self.data(using: String.Encoding.utf8) {
            return stringData.sha256()
        }
        return ""
    }
}

[ν…ŒμŠ€νŠΈ κ²°κ³Ό]

μœ„μ˜ μ½”λ“œλ‘œ μ˜ˆμ‹œλ‘œ "sha256Test"값을 ν•΄μ‹± μ²˜λ¦¬ν•΄ μ£Όμ—ˆλ”λ‹ˆ μ•„λž˜μ™€ 같이 값이 λ‚˜μ˜΅λ‹ˆλ‹€.

override func viewDidLoad() {
	super.viewDidLoad()
 
	var sha256Test = "sha256Test"
	print("SHA256 ν•΄μ‹± 이전 κ°’ : \(sha256Test)")
	sha256Test = sha256Test.sha256()
	print("SHA256 ν•΄μ‹± 이후 κ°’ : \(sha256Test)")
}

 


[Objective-c κ΅¬ν˜„]

#import <CommonCrypto/CommonDigest.h>

-(NSString*)sha256HashForText:(NSString*)text {
    const char* utf8chars = [text UTF8String];
    unsigned char result[CC_SHA256_DIGEST_LENGTH];
    CC_SHA256(utf8chars, (CC_LONG)strlen(utf8chars), result);

    NSMutableString *ret = [NSMutableString stringWithCapacity:CC_SHA256_DIGEST_LENGTH*2];
    for(int i = 0; i<CC_SHA256_DIGEST_LENGTH; i++) {
        [ret appendFormat:@"%02x",result[i]];
    }
    return ret;
}

[ν…ŒμŠ€νŠΈ κ²°κ³Ό]

NSLog(@"output String: %@", [self sha256HashForText: inputTextField]);
NSLog(@"output String Length: %lu", (unsigned long)sha256Output.length);

 

 

[μ•„λž˜ μ‚¬μ΄νŠΈμ—μ„œ κ²°κ³Όκ°’ 비ꡐ해보기]

https://coding.tools/kr/sha256

 

SHA256 μ•”ν˜Έν™” 온라인 도ꡬ - Coding.Tools

이 SHA256 μ•”ν˜Έν™” 온라인 λ„κ΅¬λŠ” μž…λ ₯ λ¬Έμžμ—΄μ„ κ³ μ • 된 256 λΉ„νŠΈ SHA256 λ¬Έμžμ—΄λ‘œ μ•”ν˜Έν™”ν•˜λŠ” 데 λ„μ›€μ΄λ©λ‹ˆλ‹€.

coding.tools

 

728x90
λ°˜μ‘ν˜•