Extending Vip



(Note: Current code is here viprc)

Encrypt/Decrypt buffer with named key
Can crypt entire buffer, or buffer range

# 04feb25 ljl

(setq *EncKeys '(("yek" . NIL )))

(local) (prinCmd)

(de prinCmd @
   (use (V L)
      (setq L
         (make
            (while (args)
               (setq V (next))
               (if (lst? V) (mapc '((X) (link X) (link "|--|")) V) (link V)))))
      (prCmd (make (link (chop (pack L)))))
      ))

(cmd "yek" (L Lst Cnt) # Set named encryption key
   (let (P1 NIL P2 NIL
         GetPwd '(() (pack (make
                  (use C
                     (until (sub? (setq C (getch)) "rn")
                        (link C)) ) ) ) )
         )
      (when L
         (prinCmd "Setting encryption key for [" L "]")
         (prinCmd "Enter Password:")
         (setq P1 (GetPwd))
         (prinCmd "Confirm Password:")
         (setq P2 (GetPwd))
         (ifn (or (sp? P1) (= P1 P2))
            (prinCmd "Encryption key NOT set; Password mismatch or empty")
            (if (assoc L *EncKeys)
               (con @ P1)
               (push '*EncKeys (cons L P1))
               )
            (when (= "yek" L) (sys "YEK" P1)) # Set default pwd for mapkeys
            (prinCmd "Encryption key set"))
            ) ) )

(cmd "enc" (L Lst Cnt) # Encrypt buffer using named key
   (when (sys "YEK" (cdr (assoc L *EncKeys)) )
      (prinCmd "Using encryption key for [" L "]")
      (use (EncryptedData DecryptedData)
         (setq DecryptedData (if (gt0 Cnt) (cdr (cutN Cnt)) (: buffer text)))
         (pipe
            (out '("openssl" "enc" "-aes-256-cbc" "-salt" "-pbkdf2" "-a" "-pass" "env:YEK")
                  (mapc prinl DecryptedData))
            (setq EncryptedData (rdLines))
            (if (gt0 Cnt)
               (paste (cons T EncryptedData) *@@)
               (setq *Change NIL)
               (move 'goAbs 1 (or (format *Count) 1)) # Move to top
               (setq *Change "d") # Delete...
               (move 'goAbs 1 (or (format *Count) T)) # Delete all
               (setq *Change "P") # Paste
               (paste (cons T EncryptedData) *@@) # Replace with encoded data
            ) )
   ) ))

(cmd "dec" (L Lst Cnt) # Decrypt buffer using named key
   (when (sys "YEK" (cdr (assoc L *EncKeys)) )
      (prinCmd "Using encryption key for [" L "]")
      (use (EncryptedData DecryptedData)
         (setq EncryptedData (if (gt0 Cnt) (cdr (cutN Cnt)) (: buffer text)))
         (pipe
            (out '("openssl" "enc" "-aes-256-cbc" "-d" "-pbkdf2" "-a" "-pass" "env:YEK")
               (mapc prinl EncryptedData))
            (setq DecryptedData (rdLines))
            (if (gt0 Cnt)
               (paste (cons T DecryptedData) *@@)
               (setq *Change NIL)
               (move 'goAbs 1 (or (format *Count) 1)) # Move to top
               (setq *Change "d") # Delete...
               (move 'goAbs 1 (or (format *Count) T)) # Delete all
               (setq *Change "P") # Paste
               (paste (cons T DecryptedData) *@@) # Replace with encoded data
               ) ) ) ))

POST buffer to 0x0.st
Shows POST response in scratch buffer

(cmd "zxz" (L Lst Cnt)
   (let (RndN '((N) (setq N (or N 4))
      (in "/dev/urandom"
         (pack (make (do N (link (hex (rd 1))))) )
      ) ) )
      (pipe
         (out '("curl" "-i" "-Ffile=@-" "https://0x0.st")
#      (out '("cat" "-")
            (mapc prinl (: buffer text)) )
         (scratch (tmp (pack "zxz-" (RndN)))
            (rdLines))
         ) ) )

Buffer navigation

# Use "sed -n l" at terminal to get scancodes for remapping
# ctrl+pgup  "^[[5;5~"  ctrl+pgdn   "^[[6;5~"
# ctrl+up    "^[[1;5A"  ctrl+down   "^[[1;5B"
# ctrl+left  "^[[1;5D"  ctrl+right  "^[[1;5C"
# up         "^[[A"     down        "^[[B"
# left       "^[[D"     right       "^[[C"

(map+ "^[[5;5~" (move 'goUp (/ (: lines) 2)))
(map+ "^[[6;5~" (move 'goDown (/ (: lines) 2)))

(map+ "\e[D" "h")
(map+ "\e[C" "l")

(map+ "^[[1;5D" (scLeft) (redraw))
(map+ "^[[1;5C" (scRight) (redraw))

(map+ "^[[1;5A" (move 'goUp 1))
(map+ "^[[1;5B" (move 'goDown 1))

# Quick buffer crypt using the key defined in "yek"
(map+q "E" ":enc yek\r" )
(map+q "D" ":dec yek\r" )

(map+ "^h" (nextBuf T) )   # [ctrl+h] Previous buffer
(map+ "^l" (nextBuf) )     # [ctrl+l] Next Buffer

(map+q "1" ":1\r")
(map+q "2" ":2\r")
(map+q "3" ":3\r")
(map+q "4" ":4\r")
(map+q "5" ":5\r")
(map+q "6" ":6\r")
(map+q "7" ":7\r")
(map+q "l" ":ls\r")



Goto Top

https://picolisp.com/wiki/?vipexampl

05feb25    llawrence