-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
aabbce5
commit bf0bacf
Showing
22 changed files
with
876 additions
and
510 deletions.
There are no files selected for viewing
37 changes: 21 additions & 16 deletions
37
Sources/ECMASwift/API/Fetch/Blob.swift → Sources/ECMASwift/API/Blob/Blob.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import CommonCrypto | ||
import JavaScriptCore | ||
|
||
@objc protocol SubtleCryptoExports: JSExport { | ||
func digest(_ algorithm: String, _ data: [UInt8]) -> [UInt8]? | ||
func encrypt(_ algorithm: String, _ key: [UInt8], _ iv: [UInt8], _ data: [UInt8]) -> [UInt8]? | ||
func decrypt(_ algorithm: String, _ key: [UInt8], _ iv: [UInt8], _ data: [UInt8]) -> [UInt8]? | ||
} | ||
|
||
/// This implmenets the `SubtleCrypto` browser API. | ||
/// | ||
/// Reference: [SubtleCrypto Reference on MDN](https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto) | ||
@objc final class SubtleCrypto: NSObject, SubtleCryptoExports { | ||
func digest(_ algorithm: String, _ data: [UInt8]) -> [UInt8]? { | ||
guard algorithm == "SHA-256" else { return nil } | ||
var hash = [UInt8](repeating: 0, count: Int(CC_SHA256_DIGEST_LENGTH)) | ||
_ = data.withUnsafeBytes { | ||
CC_SHA256($0.baseAddress, CC_LONG(data.count), &hash) | ||
} | ||
return hash | ||
} | ||
|
||
func encrypt(_ algorithm: String, _ key: [UInt8], _ iv: [UInt8], _ data: [UInt8]) -> [UInt8]? { | ||
guard algorithm == "AES-GCM", key.count == kCCKeySizeAES128 else { return nil } | ||
var buffer = [UInt8](repeating: 0, count: data.count + kCCBlockSizeAES128) | ||
var numBytesEncrypted: size_t = 0 | ||
|
||
let cryptStatus = CCCrypt( | ||
CCOperation(kCCEncrypt), | ||
CCAlgorithm(kCCAlgorithmAES), | ||
CCOptions(kCCOptionPKCS7Padding), | ||
key, | ||
key.count, | ||
iv, | ||
data, | ||
data.count, | ||
&buffer, | ||
buffer.count, | ||
&numBytesEncrypted | ||
) | ||
|
||
if cryptStatus == kCCSuccess { | ||
return Array(buffer.prefix(numBytesEncrypted)) | ||
} | ||
return nil | ||
} | ||
|
||
func decrypt(_ algorithm: String, _ key: [UInt8], _ iv: [UInt8], _ data: [UInt8]) -> [UInt8]? { | ||
guard algorithm == "AES-GCM", key.count == kCCKeySizeAES128 else { return nil } | ||
var buffer = [UInt8](repeating: 0, count: data.count + kCCBlockSizeAES128) | ||
var numBytesDecrypted: size_t = 0 | ||
|
||
let cryptStatus = CCCrypt( | ||
CCOperation(kCCDecrypt), | ||
CCAlgorithm(kCCAlgorithmAES), | ||
CCOptions(kCCOptionPKCS7Padding), | ||
key, | ||
key.count, | ||
iv, | ||
data, | ||
data.count, | ||
&buffer, | ||
buffer.count, | ||
&numBytesDecrypted | ||
) | ||
|
||
if cryptStatus == kCCSuccess { | ||
return Array(buffer.prefix(numBytesDecrypted)) | ||
} | ||
return nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import JavaScriptCore | ||
|
||
@objc | ||
protocol AbortSignalExports: JSExport { | ||
var aborted: Bool { get set } | ||
} | ||
|
||
class AbortSignal: NSObject, AbortSignalExports { | ||
private var _aborted: Bool = false | ||
var aborted: Bool { | ||
get { return _aborted } | ||
set { | ||
_aborted = newValue | ||
if newValue == true { | ||
self.onAbort?() | ||
} | ||
} | ||
} | ||
|
||
var onAbort: (() -> Void)? | ||
} | ||
|
||
@objc | ||
protocol AbortControllerExports: JSExport { | ||
var signal: AbortSignal { get set } | ||
func abort() | ||
} | ||
|
||
class AbortController: NSObject, AbortControllerExports { | ||
var signal = AbortSignal() | ||
|
||
func abort() { | ||
signal.aborted = true | ||
} | ||
} | ||
|
||
struct AbortControllerAPI { | ||
func registerAPIInto(context: JSContext) { | ||
let abortControllerClass: @convention(block) () -> AbortController = { | ||
AbortController() | ||
} | ||
let abortSignalClass: @convention(block) () -> AbortSignal = { | ||
AbortSignal() | ||
} | ||
|
||
context.setObject( | ||
abortSignalClass, | ||
forKeyedSubscript: "AbortSignal" as NSString | ||
) | ||
context.setObject( | ||
abortControllerClass, | ||
forKeyedSubscript: "AbortController" as NSString | ||
) | ||
} | ||
} |
Oops, something went wrong.