Skip to content

Commit

Permalink
feat(winter-cg): crypto, atob, btoa (NativeScript#10577)
Browse files Browse the repository at this point in the history
  • Loading branch information
triniwiz committed Jul 10, 2024
1 parent 774d4e2 commit f7679d7
Show file tree
Hide file tree
Showing 146 changed files with 5,327 additions and 653 deletions.
1 change: 1 addition & 0 deletions apps/toolbox/src/main-page.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<Button text="visibility-vs-hidden" tap="{{ viewDemo }}" class="btn btn-primary btn-view-demo" />
<Button text="fs-helper" tap="{{ viewDemo }}" class="btn btn-primary btn-view-demo" />
<Button text="webview" tap="{{ viewDemo }}" class="btn btn-primary btn-view-demo" />
<Button text="winter-cg" tap="{{ viewDemo }}" class="btn btn-primary btn-view-demo" />
</StackLayout>
</ScrollView>
</StackLayout>
Expand Down
109 changes: 109 additions & 0 deletions apps/toolbox/src/pages/winter-cg.ts
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);
}
}
6 changes: 6 additions & 0 deletions apps/toolbox/src/pages/winter-cg.xml
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>
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions packages/core/globals/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,17 @@ export function initGlobal() {
global.registerModule('fetch', () => require('../fetch'));
installPolyfills('fetch', ['fetch', 'Headers', 'Request', 'Response']);

global.registerModule('wgc', () => require('../wgc'));
installPolyfills('wgc', ['atob', 'btoa']);

global.registerModule('crypto', () => require('../wgc/crypto'));
installPolyfills('crypto', ['Crypto']);

global.registerModule('subtle', () => require('../wgc/crypto/SubtleCrypto'));
installPolyfills('subtle-crypto', ['Subtle']);

global.crypto = new global.Crypto();

// global.registerModule('abortcontroller', () => require('../abortcontroller'));
// installPolyfills('abortcontroller', ['AbortController', 'AbortSignal']);

Expand Down
Binary file modified packages/core/platforms/android/widgets-release.aar
Binary file not shown.
Binary file not shown.
102 changes: 102 additions & 0 deletions packages/core/platforms/ios/NSCWinterCG.xcframework/Info.plist
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>
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 */
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"
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 not shown.
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 not shown.
Loading

0 comments on commit f7679d7

Please sign in to comment.