File tree Expand file tree Collapse file tree 3 files changed +101
-0
lines changed
Expand file tree Collapse file tree 3 files changed +101
-0
lines changed Original file line number Diff line number Diff line change 1+ import javax.crypto.Cipher
2+ import javax.crypto.KeyGenerator
3+ import javax.crypto.spec.SecretKeySpec
4+ import org.bouncycastle.jce.provider.BouncyCastleProvider
5+ import java.security.MessageDigest
6+ import java.security.Security
7+
8+ fun main () {
9+ // Java Crypto API usage
10+ val keyGenerator = KeyGenerator .getInstance(" AES" )
11+ val secretKey = keyGenerator.generateKey()
12+
13+ val cipher = Cipher .getInstance(" AES/ECB/PKCS5Padding" )
14+ cipher.init (Cipher .ENCRYPT_MODE , secretKey)
15+
16+ val plaintext = " Hello, World!" .toByteArray()
17+ val ciphertext = cipher.doFinal(plaintext)
18+
19+ println (" Encrypted: ${ciphertext.joinToString(" " ) { " %02x" .format(it) }} " )
20+
21+ // BouncyCastle usage
22+ Security .addProvider(BouncyCastleProvider ())
23+
24+ val messageDigest = MessageDigest .getInstance(" SHA-256" )
25+ val hash = messageDigest.digest(plaintext)
26+
27+ println (" SHA-256: ${hash.joinToString(" " ) { " %02x" .format(it) }} " )
28+
29+ // Korlibs Krypto usage (Kotlin Multiplatform)
30+ // Note: This would typically be in a multiplatform project
31+ // import com.soywiz.krypto.encoding.hex
32+ // import com.soywiz.krypto.hash.sha256
33+ // val kryptoHash = plaintext.sha256().hex
34+ // println("Krypto SHA-256: $kryptoHash")
35+ }
Original file line number Diff line number Diff line change 1+ #import < Foundation/Foundation.h>
2+ #import < CommonCrypto/CommonCrypto.h>
3+ #import < openssl/evp.h>
4+ #import < openssl/rsa.h>
5+
6+ int main (int argc, const char * argv[]) {
7+ @autoreleasepool {
8+ // CommonCrypto usage
9+ const char *message = " Hello, World!" ;
10+ unsigned char digest[CC_SHA256_DIGEST_LENGTH];
11+
12+ CC_SHA256 (message, (CC_LONG)strlen (message), digest);
13+
14+ printf (" SHA256: " );
15+ for (int i = 0 ; i < CC_SHA256_DIGEST_LENGTH; i++) {
16+ printf (" %02x " , digest[i]);
17+ }
18+ printf (" \n " );
19+
20+ // OpenSSL usage
21+ EVP_MD_CTX *mdctx = EVP_MD_CTX_new ();
22+ const EVP_MD *md = EVP_sha256 ();
23+
24+ EVP_DigestInit_ex (mdctx, md, NULL );
25+ EVP_DigestUpdate (mdctx, message, strlen (message));
26+
27+ unsigned int digest_len;
28+ EVP_DigestFinal_ex (mdctx, digest, &digest_len);
29+ EVP_MD_CTX_free (mdctx);
30+
31+ printf (" OpenSSL SHA256: " );
32+ for (unsigned int i = 0 ; i < digest_len; i++) {
33+ printf (" %02x " , digest[i]);
34+ }
35+ printf (" \n " );
36+ }
37+ return 0 ;
38+ }
Original file line number Diff line number Diff line change 1+ import CryptoKit
2+ import CommonCrypto
3+
4+ func main( ) {
5+ // CryptoKit usage
6+ let key = SymmetricKey ( size: . bits256)
7+ let data = " Hello, World! " . data ( using: . utf8) !
8+
9+ do {
10+ let sealedBox = try AES . GCM. seal ( data, using: key)
11+ print ( " Encrypted: \( sealedBox) " )
12+ } catch {
13+ print ( " Encryption failed: \( error) " )
14+ }
15+
16+ // CommonCrypto usage
17+ let message = " Test message "
18+ let messageData = message. data ( using: . utf8) !
19+ var digest = Data ( count: Int ( CC_SHA256_DIGEST_LENGTH) )
20+
21+ _ = digest. withUnsafeMutableBytes { digestBytes in
22+ messageData. withUnsafeBytes { messageBytes in
23+ CC_SHA256 ( messageBytes. baseAddress, CC_LONG ( messageData. count) , digestBytes. bindMemory ( to: UInt8 . self) . baseAddress)
24+ }
25+ }
26+
27+ print ( " SHA256: \( digest. map { String ( format: " %02hhx " , $0) } . joined ( ) ) " )
28+ }
You can’t perform that action at this time.
0 commit comments