cancel
Showing results forย 
Search instead forย 
Did you mean:ย 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

Create gpg file and save to AWS s3 storage in scala

sriwin
New Contributor

Hi - Could you please help me on how can I create a scala notebook to perform the below tasks

  1. Encrypt a text file using the gpg
  2. Upload the file to amazon s3 storage
  3. verify the file exists in amazon s3
  4. decrypt the encrypted file to verify no issues

Appreciate your support

Thanks

Sriwin

1 ACCEPTED SOLUTION

Accepted Solutions

Kaniz_Fatma
Community Manager
Community Manager

Hi @sriwin pโ€‹ , There is encrypt/decrypt file test case code from PR to the https://github.com/sbt/sbt-pgp repository. It provides an example of usage of PGP file encryption/decryption with :

package com.jsuereth.pgp
 
 import org.specs2.mutable._
 import sbt.io.IO
 
 import java.io.{BufferedWriter, File, FileWriter}
 
 class KeyGenSpec extends Specification {
 PGP.init()
 
 ...
 
 val user = "Test User <test@user.com>"
 val pw: Array[Char] = "test-pw".toCharArray
 val (pub, sec) = PGP.makeNewKeyRings(user, pw)
 
 "encrypt and decrypt file" in {
  IO withTemporaryDirectory { dir =>
    val fileContent = "Just one string"
    val testFile1 = new File(dir, "test1.txt")
    val testFile2 = new File(dir, "test2.txt")
 
    // original file
    val bw1 = new BufferedWriter(new FileWriter(testFile1))
    bw1.write(fileContent)
    bw1.close()
 
    val source1 = scala.io.Source.fromFile(testFile1.getAbsolutePath)
    val lines1 = try source1.mkString finally source1.close()
    //System.out.println(lines1)
 
    // encrypted -> decrypted file preparation
    val bw2 = new BufferedWriter(new FileWriter(testFile2))
    bw2.write(fileContent)
    bw2.close()
 
    val testFileEncrypted = new File(dir, "testEncrypted.txt")
    sec.publicKey.encryptFile(testFile2, testFileEncrypted)
    testFile2.delete()
    sec.secretKey.decryptFile(testFileEncrypted, pw)
 
    val source2 = scala.io.Source.fromFile(testFile2.getAbsolutePath)
    val lines2 = try source2.mkString finally source2.close()
    //System.out.println(lines2)
 
    lines1 must equalTo(lines2)
  }
 }
...
}

Source:-https://github.com/CTiPKA/sbt-pgp/blob/develop/gpg-library/src/test/scala/com/jsuereth/pgp/KeyGenSpec.scala#L34

View solution in original post

2 REPLIES 2

Anonymous
Not applicable

Hello! My name is Piper and I'm a community moderator for Databricks. Thanks for your question. Let's give it a bit more to see what our members have to say. If not, we'll circle back around.

Kaniz_Fatma
Community Manager
Community Manager

Hi @sriwin pโ€‹ , There is encrypt/decrypt file test case code from PR to the https://github.com/sbt/sbt-pgp repository. It provides an example of usage of PGP file encryption/decryption with :

package com.jsuereth.pgp
 
 import org.specs2.mutable._
 import sbt.io.IO
 
 import java.io.{BufferedWriter, File, FileWriter}
 
 class KeyGenSpec extends Specification {
 PGP.init()
 
 ...
 
 val user = "Test User <test@user.com>"
 val pw: Array[Char] = "test-pw".toCharArray
 val (pub, sec) = PGP.makeNewKeyRings(user, pw)
 
 "encrypt and decrypt file" in {
  IO withTemporaryDirectory { dir =>
    val fileContent = "Just one string"
    val testFile1 = new File(dir, "test1.txt")
    val testFile2 = new File(dir, "test2.txt")
 
    // original file
    val bw1 = new BufferedWriter(new FileWriter(testFile1))
    bw1.write(fileContent)
    bw1.close()
 
    val source1 = scala.io.Source.fromFile(testFile1.getAbsolutePath)
    val lines1 = try source1.mkString finally source1.close()
    //System.out.println(lines1)
 
    // encrypted -> decrypted file preparation
    val bw2 = new BufferedWriter(new FileWriter(testFile2))
    bw2.write(fileContent)
    bw2.close()
 
    val testFileEncrypted = new File(dir, "testEncrypted.txt")
    sec.publicKey.encryptFile(testFile2, testFileEncrypted)
    testFile2.delete()
    sec.secretKey.decryptFile(testFileEncrypted, pw)
 
    val source2 = scala.io.Source.fromFile(testFile2.getAbsolutePath)
    val lines2 = try source2.mkString finally source2.close()
    //System.out.println(lines2)
 
    lines1 must equalTo(lines2)
  }
 }
...
}

Source:-https://github.com/CTiPKA/sbt-pgp/blob/develop/gpg-library/src/test/scala/com/jsuereth/pgp/KeyGenSpec.scala#L34

Connect with Databricks Users in Your Area

Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you wonโ€™t want to miss the chance to attend and share knowledge.

If there isnโ€™t a group near you, start one and help create a community that brings people together.

Request a New Group