forked from NativeScript/NativeScript
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(winter-cg): crypto, atob, btoa (NativeScript#10577)
- Loading branch information
Showing
146 changed files
with
5,327 additions
and
653 deletions.
There are no files selected for viewing
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,109 @@ | ||
import { Page, EventData, Application, File, Folder, knownFolders, path, getFileAccess, Utils, Screen, Http, AndroidDirectory, ImageSource, alert } from '@nativescript/core'; | ||
|
||
let page: Page; | ||
|
||
export function navigatingTo(args: EventData) { | ||
page = <Page>args.object; | ||
run(); | ||
} | ||
|
||
function run() { | ||
console.log(crypto.randomUUID()); | ||
|
||
const buf = new BigUint64Array(3); | ||
|
||
crypto.getRandomValues(buf); | ||
|
||
console.log(buf); | ||
|
||
const text = 'An obscure body in the S-K System, your majesty. The inhabitants refer to it as the planet Earth.'; | ||
|
||
async function digestMessage(message) { | ||
const encoder = new TextEncoder(); | ||
const data = encoder.encode(message); | ||
console.time('digestMessage'); | ||
const hash = await crypto.subtle.digest('SHA-256', data); | ||
console.timeEnd('digestMessage'); | ||
return hash; | ||
} | ||
|
||
digestMessage(text).then((digestBuffer) => { | ||
console.log(digestBuffer.byteLength); | ||
console.log(new Uint8Array(digestBuffer)); | ||
}); | ||
|
||
gen_hmac(); | ||
|
||
gen_rsa_oaep(); | ||
} | ||
|
||
export function encodeDecode() { | ||
const encoded = btoa('Osei'); | ||
|
||
console.log(encoded); | ||
|
||
console.log(atob(encoded) === 'Osei'); | ||
} | ||
|
||
async function gen_hmac() { | ||
let message = 'Hello World'; | ||
let enc = new TextEncoder(); | ||
const encoded = enc.encode(message); | ||
|
||
const key = await crypto.subtle.generateKey( | ||
{ | ||
name: 'HMAC', | ||
hash: { name: 'SHA-512' }, | ||
}, | ||
true, | ||
['sign', 'verify'], | ||
); | ||
|
||
const signature = await crypto.subtle.sign('HMAC', key, encoded); | ||
|
||
let result = await crypto.subtle.verify('HMAC', key, signature, encoded); | ||
|
||
console.log('gen_hmac is valid? ', result); | ||
} | ||
|
||
async function gen_rsa_oaep() { | ||
let message = 'Hello World'; | ||
let enc = new TextEncoder(); | ||
const encoded = enc.encode(message); | ||
|
||
const kp = await crypto.subtle.generateKey( | ||
{ | ||
name: 'RSA-OAEP', | ||
modulusLength: 4096, | ||
publicExponent: new Uint8Array([1, 0, 1]), | ||
hash: 'SHA-256', | ||
}, | ||
true, | ||
['encrypt', 'decrypt'], | ||
); | ||
|
||
try { | ||
const ciphertext = await crypto.subtle.encrypt( | ||
{ | ||
name: 'RSA-OAEP', | ||
}, | ||
kp.publicKey, | ||
encoded, | ||
); | ||
|
||
let decrypted = await crypto.subtle.decrypt( | ||
{ | ||
name: 'RSA-OAEP', | ||
}, | ||
kp.privateKey, | ||
ciphertext, | ||
); | ||
|
||
let dec = new TextDecoder(); | ||
const decryptedValue = dec.decode(decrypted); | ||
|
||
console.log('decryptedValue', decryptedValue, decryptedValue === message); | ||
} catch (error) { | ||
console.log('gen: error', error); | ||
} | ||
} |
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,6 @@ | ||
<Page xmlns="http://schemas.nativescript.org/tns.xsd" navigatingTo="navigatingTo" class="page"> | ||
|
||
<StackLayout> | ||
<Button text="Btoa y Atob" tap="encodeDecode" /> | ||
</StackLayout> | ||
</Page> |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Binary file not shown.
Binary file not shown.
102 changes: 102 additions & 0 deletions
102
packages/core/platforms/ios/NSCWinterCG.xcframework/Info.plist
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,102 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | ||
<plist version="1.0"> | ||
<dict> | ||
<key>AvailableLibraries</key> | ||
<array> | ||
<dict> | ||
<key>BinaryPath</key> | ||
<string>NSCWinterCG.framework/NSCWinterCG</string> | ||
<key>DebugSymbolsPath</key> | ||
<string>dSYMs</string> | ||
<key>LibraryIdentifier</key> | ||
<string>ios-arm64</string> | ||
<key>LibraryPath</key> | ||
<string>NSCWinterCG.framework</string> | ||
<key>SupportedArchitectures</key> | ||
<array> | ||
<string>arm64</string> | ||
</array> | ||
<key>SupportedPlatform</key> | ||
<string>ios</string> | ||
</dict> | ||
<dict> | ||
<key>BinaryPath</key> | ||
<string>NSCWinterCG.framework/NSCWinterCG</string> | ||
<key>DebugSymbolsPath</key> | ||
<string>dSYMs</string> | ||
<key>LibraryIdentifier</key> | ||
<string>xros-arm64_x86_64-simulator</string> | ||
<key>LibraryPath</key> | ||
<string>NSCWinterCG.framework</string> | ||
<key>SupportedArchitectures</key> | ||
<array> | ||
<string>arm64</string> | ||
<string>x86_64</string> | ||
</array> | ||
<key>SupportedPlatform</key> | ||
<string>xros</string> | ||
<key>SupportedPlatformVariant</key> | ||
<string>simulator</string> | ||
</dict> | ||
<dict> | ||
<key>BinaryPath</key> | ||
<string>NSCWinterCG.framework/NSCWinterCG</string> | ||
<key>DebugSymbolsPath</key> | ||
<string>dSYMs</string> | ||
<key>LibraryIdentifier</key> | ||
<string>ios-arm64_x86_64-simulator</string> | ||
<key>LibraryPath</key> | ||
<string>NSCWinterCG.framework</string> | ||
<key>SupportedArchitectures</key> | ||
<array> | ||
<string>arm64</string> | ||
<string>x86_64</string> | ||
</array> | ||
<key>SupportedPlatform</key> | ||
<string>ios</string> | ||
<key>SupportedPlatformVariant</key> | ||
<string>simulator</string> | ||
</dict> | ||
<dict> | ||
<key>BinaryPath</key> | ||
<string>NSCWinterCG.framework/Versions/A/NSCWinterCG</string> | ||
<key>DebugSymbolsPath</key> | ||
<string>dSYMs</string> | ||
<key>LibraryIdentifier</key> | ||
<string>ios-arm64_x86_64-maccatalyst</string> | ||
<key>LibraryPath</key> | ||
<string>NSCWinterCG.framework</string> | ||
<key>SupportedArchitectures</key> | ||
<array> | ||
<string>arm64</string> | ||
<string>x86_64</string> | ||
</array> | ||
<key>SupportedPlatform</key> | ||
<string>ios</string> | ||
<key>SupportedPlatformVariant</key> | ||
<string>maccatalyst</string> | ||
</dict> | ||
<dict> | ||
<key>BinaryPath</key> | ||
<string>NSCWinterCG.framework/NSCWinterCG</string> | ||
<key>DebugSymbolsPath</key> | ||
<string>dSYMs</string> | ||
<key>LibraryIdentifier</key> | ||
<string>xros-arm64</string> | ||
<key>LibraryPath</key> | ||
<string>NSCWinterCG.framework</string> | ||
<key>SupportedArchitectures</key> | ||
<array> | ||
<string>arm64</string> | ||
</array> | ||
<key>SupportedPlatform</key> | ||
<string>xros</string> | ||
</dict> | ||
</array> | ||
<key>CFBundlePackageType</key> | ||
<string>XFWK</string> | ||
<key>XCFrameworkFormatVersion</key> | ||
<string>1.0</string> | ||
</dict> | ||
</plist> |
63 changes: 63 additions & 0 deletions
63
...platforms/ios/NSCWinterCG.xcframework/ios-arm64/NSCWinterCG.framework/Headers/NSCCrypto.h
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,63 @@ | ||
// | ||
// NSCCrypto.h | ||
// NSCWinterCG | ||
// | ||
// Created by Osei Fortune on 03/07/2024. | ||
// Copyright © 2024 NativeScript. All rights reserved. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
#import <CommonCrypto/CommonCrypto.h> | ||
#import <Security/Security.h> | ||
#import <Security/SecItem.h> | ||
#ifndef NSCCrypto_h | ||
#define NSCCrypto_h | ||
|
||
typedef enum : NSUInteger { | ||
kNSCCryptoRSASSA_PKCS1_v1_5, | ||
kNSCCryptoRSA_PSS, | ||
kNSCCryptoRSA_OAEP, | ||
} NSCCryptoRsaHashedKeyGenParamsName; | ||
|
||
|
||
typedef enum : NSUInteger { | ||
kNSCCryptoHashSHA1, | ||
kNSCCryptoHashSHA256, | ||
kNSCCryptoHashSHA384, | ||
kNSCCryptoHashSHA512 | ||
} NSCCryptoHash; | ||
|
||
|
||
typedef enum : NSUInteger { | ||
kNSCCryptoDecrypt, | ||
kNSCCryptoEncrypt, | ||
kNSCCryptoSign, | ||
kNSCCryptoVerify, | ||
kNSCCryptoDeriveKey, | ||
kNSCCryptoDeriveBits, | ||
kNSCCryptoWrapKey, | ||
kNSCCryptoUnwrapKey, | ||
} NSCCryptoKeyUsages; | ||
|
||
|
||
|
||
@interface NSCCryptoKeyPair : NSObject | ||
@property (nonatomic, nonnull) SecKeyRef privateKey; | ||
@property (nonatomic, nonnull) SecKeyRef publicKey; | ||
-(nonnull id)initWithPrivateKey:(nonnull SecKeyRef)privKey andPublicKey:(nonnull SecKeyRef)pubKey; | ||
@end | ||
|
||
@interface NSCCrypto:NSObject | ||
+ (nonnull NSString*)randomUUID; | ||
+ (nullable NSString*)getRandomValues:(nonnull void*)buffer length:(unsigned int)length; | ||
+ (nullable NSData*)digest:(nonnull void*)data length:(unsigned int)length mode:(int)mode; | ||
+ (nullable NSData *)generateKeyHmac:(int)hash length:(int)length; | ||
+ (nullable NSData *)signHmac:(nonnull NSData*)key hash:(NSCCryptoHash)hash data:(nonnull NSData*)data; | ||
+ (BOOL)verifyHmac:(nonnull NSData*)key hash:(NSCCryptoHash)hash signature:(nonnull NSData*)signature data:(nonnull NSData*)data; | ||
|
||
+ (nullable NSCCryptoKeyPair*)generateKeyRsa:(NSCCryptoRsaHashedKeyGenParamsName)name modulusLength:(unsigned int)modulusLength publicExponent:(nullable void*)exponent size:(unsigned int)size hash:(NSCCryptoHash)hash extractable:(BOOL)extractable keyUsages:(nonnull NSArray*) usages; | ||
+ (nullable NSData *)encryptRsa:(BOOL)isPrivate key:(nonnull NSCCryptoKeyPair *)key hash:(NSCCryptoHash)hash data:(nonnull void*)data size:(unsigned int) size; | ||
|
||
+ (nullable NSData *)decryptRsa:(BOOL)isPrivate key:(nonnull NSCCryptoKeyPair *)key hash:(NSCCryptoHash)hash data:(nonnull void*)data size:(unsigned int) size; | ||
@end | ||
#endif /* NSCCrypto_h */ |
20 changes: 20 additions & 0 deletions
20
...atforms/ios/NSCWinterCG.xcframework/ios-arm64/NSCWinterCG.framework/Headers/NSCWinterCG.h
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,20 @@ | ||
// | ||
// NSCWinterCG.h | ||
// NSCWinterCG | ||
// | ||
// Created by Osei Fortune and Nathan Walker on 07/09/2024. | ||
// Copyright © 2024 NativeScript. All rights reserved. | ||
// | ||
|
||
#import <UIKit/UIKit.h> | ||
|
||
//! Project version number for NSCWinterCG. | ||
FOUNDATION_EXPORT double NSCWinterCGVersionNumber; | ||
|
||
//! Project version string for NSCWinterCG. | ||
FOUNDATION_EXPORT const unsigned char NSCWinterCGVersionString[]; | ||
|
||
// In this header, you should import all the public headers of your framework using statements like #import <NSCWinterCG/PublicHeader.h> | ||
|
||
#import "NSCCrypto.h" | ||
#import "NSString+WinterCG.h" |
21 changes: 21 additions & 0 deletions
21
...s/ios/NSCWinterCG.xcframework/ios-arm64/NSCWinterCG.framework/Headers/NSString+WinterCG.h
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,21 @@ | ||
// | ||
// NSString+WinterCG.h | ||
// NSCWinterCG | ||
// | ||
// Created by Osei Fortune and Nathan Walker on 07/09/2024. | ||
// Copyright © 2024 NativeScript. All rights reserved. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
NS_ASSUME_NONNULL_BEGIN | ||
|
||
@interface NSString (WinterCG) | ||
|
||
+ (NSString*)atob:(nonnull NSString*)data; | ||
|
||
+ (NSString*)btoa:(nonnull NSString*)stringToEncode; | ||
|
||
@end | ||
|
||
NS_ASSUME_NONNULL_END |
Binary file added
BIN
+792 Bytes
...ges/core/platforms/ios/NSCWinterCG.xcframework/ios-arm64/NSCWinterCG.framework/Info.plist
Binary file not shown.
6 changes: 6 additions & 0 deletions
6
...orms/ios/NSCWinterCG.xcframework/ios-arm64/NSCWinterCG.framework/Modules/module.modulemap
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,6 @@ | ||
framework module NSCWinterCG { | ||
umbrella header "NSCWinterCG.h" | ||
export * | ||
|
||
module * { export * } | ||
} |
Binary file added
BIN
+57.2 KB
...es/core/platforms/ios/NSCWinterCG.xcframework/ios-arm64/NSCWinterCG.framework/NSCWinterCG
Binary file not shown.
Oops, something went wrong.