Add documentation comments; don't return an optional from the FileHandle method

This commit is contained in:
Nate Weaver committed 2020-05-01 19:23:58 -05:00
1 parent c8d5d18ce0
commit aff67ceda8
1 file changed
+10 -1
+10 -1
View File
@@ -11,7 +11,11 @@ import CommonCrypto
extension FileHandle {
func sha256String() -> String? {
/// Calculate the SHA-256 hash of the file referenced by the file handle.
///
/// - Returns: The SHA-256 hash of the file (as a hexadecimal string).
func sha256String() -> String {
// This uses CommonCrypto instead of CryptoKit so it can work on macOS < 10.15
var context = CC_SHA256_CTX()
CC_SHA256_Init(&context)
@@ -36,7 +40,12 @@ extension FileHandle {
extension URL {
/// Calculates the SHA-256 hash of the file referened by the URL.
///
/// - Returns: The SHA-256 hash of the file (as a hexadecimal string), or `nil` if
/// the URL doesn't point to a file.
func sha256String() -> String? {
guard self.isFileURL else { return nil }
guard let filehandle = try? FileHandle(forReadingFrom: self) else { return nil }
return filehandle.sha256String()