Coder Social home page Coder Social logo

etienne-martin / webcrypto.swift Goto Github PK

View Code? Open in Web Editor NEW
19.0 5.0 12.0 60 KB

A small collection of cryptographic functions based on the JavaScript WebCrypto API.

License: Other

Swift 86.23% JavaScript 3.39% HTML 10.38%
swift aes cryptography hash digest cipher encryption decryption openssl sha1

webcrypto.swift's People

Contributors

etienne-martin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

webcrypto.swift's Issues

Get crypted String

Hello
I can get hex string from crypted data, but I want to get crypted text.
How can I get crypted text from crypted data?
And how can I convert crypted text to crypted data for decrypt?
Thanks.

CryptoJS result VS WebCrypto

After having used CryptoJS for a while I recently tried to implement password hashing on iOS via Swift and gave WebCrypto a try. CryptoJS uses a wordarray when hashing with sha512 and stringify with base64 option gets applied, leading to different results than using any other sha512 hashing method. The output of WebCrypto swift is the same as all other methods so it’s correct, but I wonder if there is any way for this library to replicate the output of CryptoJS, especially since it’s based on CryptoJS Swift.

Is there any way to get the same result from Webcrypto swift with a sha512 like CryptoJS puts out?

Cannot decrypt from AES encrypted code

Hi Etienne,
I cannot decrypt an AES encrypted string. Here follows the code I wrote:

let code = "6yVJAXlqQGli1wJWlcQoFeQVD/BQR3TDQNJZe1/gQQ5tCN6pRe0gOlAkv1CWTA3ZjXdntMooxI7bm9rphV+DyqGyXRo34Ou38v+GqnSfY5N3m30GH3ieUa20RxdX9cUmtQCxJaDC47C79ybWkFRuXWHmqONVFxXVe4ZqyZBDy+a2zLxLufHEB9mJkH5DEID1d6bcRteONAb7KYbFkJLGbQ=="
          
let input = Data(code.utf8)
let plainPassword="pd$83j@Fs3s13bZP";
let plainIV="aNdh284629324F32";
        
let key = crypto.hexEncodedStringFromData(Data(plainPassword.utf8));
let iv = crypto.hexEncodedStringFromData(Data(plainIV.utf8));
           
self.crypto.decrypt(data: input, key: key, iv: iv, callback: {(decrypted: Data?, error: Error?) in
       print(String(data: decrypted!, encoding: .utf8)!)
})

This is the result that I have executing the above code:

««Ð8ë;xv1t!Õ(öaDM×�#� ?�Ú� §oà�Á�� Ï×j�ë½�ÂÙ'¶Ý¢£ÇKËOU�Tü�v2�\ �-­@�ë3å±gÁý_Ìc°ÃØFZ�@@ö(}Ú HwXSj��±>±èYÆ^ÑÑY^ Õ� ëú&�L�qí]r[ ú ¨3Æ î%�õDÞ£g9|¿ãËÓ{G km'& ÝÕÆïw�/%[¯�j& ;5Åw¡dK ��}·±� 6³�S&�æ6§C �¶óþæþ�0hä
[÷ {¾ç�ñPPîR>Á\5pj�z �Ø$� $Ø_0�Ûýã ÙK ÕÔÓ�½¸.Pþ÷t�ÀÊQ Þ� ¯YÈ@ m�� VzÉɶ:î}Æ6�&ÿàj¬

I wrote the same example in js (you can find the code at https://jsfiddle.net/jwz1dx4a/) and the result is ok:

{"user_info":{"id":4037,"username":"pippo","company_name":"Pippo","license_number":"FK01455-3","password":"pippo123"},"code":"200","msg":"login ok"}

Can you help me out?

Thanks, Vincenzo

Want to decrypt with password for 128 bit encryption?

You mentioned: WebCrypto.swift supports AES-128, AES-192, and AES-256. It will pick the variant by the size of the key you pass in. If you use a password, AES-256 will be used.

But the content which i want to decrypt is AES 128 ECB pkcs7 with password. How to decrypt with password for 128 bit with your library?

About the CryptoSwift performance result you compare. The value It's not right.

Hi,

I've noticed that you compare performance to CryptoSwift here: https://github.com/etienne-martin/WebCrypto.swift#performance

I'm concerned about the value: CryptoSwift: 24366ms
I'd like you to ask about the tests you performed as it's significantly higher value than the framework performance tests.

Based on the given test: https://github.com/krzyzanowskim/CryptoSwift/blob/06f3cf38439abed5487dd8b4b30ebc1daa08cb3e/Tests/CryptoSwiftTests/AESTests.swift#L370 (adjusted to 10mb,AES-256), the expected result is more like 753ms on i7 mac.

screen shot 2018-02-07 at 03 17 43

I assume you performed your test with Debug build, that is unoptimized and expected to be x1000 slower than the Release code. That's the nature of Swift language.

I kindly ask you to update the value or remove the CryptoSwift from the comparison.

Convery crypto.js algorithm to Swift

Hi, I would like to know how to use the below js method to work in Swift:

let key = "Q8pTgdOC7h25qKVncJlzDA=="
let message = "TestAnitaa009"
let base64Key = CryptoJS.enc.Base64.parse(key);

var iv = base64Key

let encryptedValue = CryptoJS.AES.encrypt(message, base64Key, {
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7,
iv: base64Key
});

//The above code outputs = Oz9b31UvNTW8rFfGUOfB5A==

I tried using this library to help to do the same in Swift. But I am not able to. Any help is appreciated. Thanks a lot

Can't decrypt the aes 128 encrypted string

I tried to use WebCrypto.swift with following example but i am not getting correct decrypted answer, as Encrypted string in given code (input) is "good morning",

let input = "SqPHeh4rWB0zt0cRfv/9uw==" 
let data = Data(input.utf8) 
let key = "password12345678" 
let keydata = Data(key.utf8).base64EncodedString() 
let newKey = crypto.hexEncodedStringFromData(Data(keydata.utf8)) 
let iv = "5D9r9ZVzEYYgha93/aUK2w==" 
let ivdata = Data(iv.utf8).base64EncodedString() 

crypto.decrypt(data: data, key: newKey, iv: ivdata) { (decrypted, error) in 
    if decrypted != nil { 
        print(decrypted!) // I am getting wrong data here 
    }else { 
        print(error!) 
    } 
}

Can you please explain this sample example?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.