//
/*-
 * ---license-start
 * eu-digital-green-certificates / dgca-app-core-ios
 * ---
 * Copyright (C) 2021 T-Systems International GmbH and all other contributors
 * ---
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ---license-end
 */
//
//  SHA256.swift
//  DGCAVerifier
//
//  Created by Yannick Spreen on 4/22/21.
//
//  https://stackoverflow.com/a/38788437/2585092
//
        
import Foundation
import CommonCrypto

public struct SHA256 {
  public static func digest(input: NSData) -> Data {
    let digestLength = Int(CC_SHA256_DIGEST_LENGTH)
    var hash = [UInt8](repeating: 0, count: digestLength)
    CC_SHA256(input.bytes, UInt32(input.length), &hash)
    return Data(NSData(bytes: hash, length: digestLength))
  }
  public static func stringDigest(input: Data) -> String {
    return digest(input: input as NSData).base64EncodedString()
  }
}
