Skip to content

Commit

Permalink
Add AES-128 encryption and Hex encoding (#63)
Browse files Browse the repository at this point in the history
* add hex encoding

* add aes-128 encryption

* update test project

* update readme and haxelib
  • Loading branch information
Geokureli authored Aug 16, 2022
1 parent 4595036 commit 446add9
Show file tree
Hide file tree
Showing 8 changed files with 620 additions and 71 deletions.
15 changes: 9 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,17 @@ if (NG.core.loggedIn == false)
Setting the encryption method is easy, just call:

```haxe
NG.core.initEncryption("encryption key", someEncryptionCipher, someEncryptionFormat);
NG.core.setupEncryption("encryption key", AES_128, BASE_64);
```

Encryption Ciphers:
- **io.newgrounds.crypto.Cipher.NONE**
- **io.newgrounds.crypto.Cipher.AES-128** (not implemented)
- **io.newgrounds.crypto.Cipher.RC4** (default)
- **io.newgrounds.crypto.Cipher.AES-128**(default)
- **io.newgrounds.crypto.Cipher.RC4**

Encryption Ciphers:
- **io.newgrounds.crypto.EncryptionFormat.BASE_64** (default)
- **io.newgrounds.crypto.EncryptionFormat.HEX** (not implemented)
- **io.newgrounds.crypto.EncryptionFormat.HEX**

You can also use your own encryption method - if you're some kind of crypto-god from The Matrix -
by directly setting NG.core.encryptionHandler
Expand Down Expand Up @@ -228,8 +228,11 @@ NG.core.medal.unlock(id).queue();
```

## TODO
- AES-128 encryption
- Hex encoding
- ~~AES-128 encryption~~
- ~~Hex encoding~~
- Enable AES-128 and Hex in the GUI test project
- Pretty up the GUI test project in general
- Replace successCallbacks and failCallbacks with resultCallbacks (2.0.0)
- kill all humans
- flash API assets
- ad viewer - not supported in ng.io v3
Expand Down
4 changes: 2 additions & 2 deletions haxelib.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{ "name" : "newgrounds"
, "description" : "Newgrounds API for haxe"
, "version" : "1.2.0"
, "releasenote" : "Adds CloudSaveFeatures"
, "version" : "1.3.0"
, "releasenote" : "Adds Hex encoding and AES-128 encryption"
, "url" : "https://newgrounds.io/"
, "classPath" : "lib/Source"
, "license" : "MIT"
Expand Down
51 changes: 20 additions & 31 deletions lib/Source/io/newgrounds/NGLite.hx
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package io.newgrounds;

import haxe.crypto.Base64;
import haxe.io.Bytes;
import haxe.PosInfos;

import io.newgrounds.Call.ICallable;
import io.newgrounds.components.ComponentList;
import io.newgrounds.crypto.EncryptionFormat;
import io.newgrounds.crypto.EncodingFormat;
import io.newgrounds.crypto.Cipher;
import io.newgrounds.crypto.Rc4;
import io.newgrounds.objects.Error;
import io.newgrounds.objects.events.Response;
import io.newgrounds.objects.events.Result.ResultBase;
Expand Down Expand Up @@ -251,39 +249,30 @@ class NGLite {

// -------------------------------------------------------------------------------------------
// ENCRYPTION
// -------------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------------


/** Sets */
public function initEncryption
public function setupEncryption
( key :String
, cipher:Cipher = Cipher.RC4
, format:EncryptionFormat = EncryptionFormat.BASE_64
):Void {
, cipher:Cipher = AES_128
, format:EncodingFormat = BASE_64
) {

encryptionHandler = null;

if (cipher == Cipher.NONE)
encryptionHandler = null;
else if (cipher == Cipher.RC4)
encryptionHandler = encryptRc4.bind(key, format);
else
throw "aes not yet implemented";
var encrypt = cipher.generateEncrypter(format.decode(key));
if (encrypt != null) {

encryptionHandler = function (data) {

return format.encode(encrypt(Bytes.ofString(data)));
}
}
}

function encryptRc4(key:String, format:EncryptionFormat, data:String):String {

if (format == EncryptionFormat.HEX)
throw "hex format not yet implemented";

var keyBytes:Bytes;
if (format == EncryptionFormat.BASE_64)
keyBytes = Base64.decode(key);
else
keyBytes = null;//TODO
@:deprecated("initEncryption is deprecated, use `setupEncryption`, now that AES-128 is the default")
inline public function initEncryption(key, cipher = RC4, format = BASE_64) {

var dataBytes = new Rc4(keyBytes).crypt(Bytes.ofString(data));

if (format == EncryptionFormat.BASE_64)
return Base64.encode(dataBytes);

return null;
setupEncryption(key, cipher, format);
}
}
Loading

0 comments on commit 446add9

Please sign in to comment.