Skip to content

Instantly share code, notes, and snippets.

@kaimingguo
Created January 30, 2024 03:05
Show Gist options
  • Save kaimingguo/6399a79c3d390086a9d0e92fcc3660d5 to your computer and use it in GitHub Desktop.
Save kaimingguo/6399a79c3d390086a9d0e92fcc3660d5 to your computer and use it in GitHub Desktop.
Example of adding a password to a specific presentation in Go
package main
import (
"math/rand"
"path/filepath"
"time"
"github.com/go-ole/go-ole"
"github.com/go-ole/go-ole/oleutil"
"github.com/scjalliance/comshim"
)
// Specifies a tri-state Boolean value
const (
msoTrue int = iota - 1
msoFalse
)
const (
letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
specialBytes = "!#$%&()*+-./:;<=>?@[]^_`{|}~"
numberBytes = "0123456789"
)
func init() {
rand.New(rand.NewSource(time.Now().UnixNano()))
}
func main() {
comshim.Add(1)
defer comshim.Done()
unknown, err := oleutil.CreateObject("PowerPoint.Application")
if err != nil {
panic(err)
}
defer func() {
_ = unknown.Release()
unknown = nil
}()
app, err := unknown.QueryInterface(ole.IID_IDispatch)
if err != nil {
panic(err)
}
defer func() {
_ = app.Release()
app = nil
}()
presentations := oleutil.MustGetProperty(app, "Presentations")
defer presentations.Clear()
fileName, err := filepath.Abs("file.pptx")
if err != nil {
panic(err)
}
presentation, err := oleutil.CallMethod(presentations.ToIDispatch(), "Open", fileName, msoFalse, msoFalse, msoFalse)
if err != nil {
panic(err)
}
defer presentation.Clear()
password := generatePassword(16, true, false, true)
_ = oleutil.MustPutProperty(presentation.ToIDispatch(), "Password", password)
_ = oleutil.MustCallMethod(presentation.ToIDispatch(), "Save")
_ = oleutil.MustCallMethod(presentation.ToIDispatch(), "Close")
println(password)
}
func generatePassword(length int, useLetters, useSpecial, useNumber bool) string {
b := make([]byte, length)
password := make([]byte, 0)
if useLetters {
password = append(password, letterBytes...)
}
if useSpecial {
password = append(password, specialBytes...)
}
if useNumber {
password = append(password, numberBytes...)
}
for i := range b {
b[i] = password[rand.Intn(len(password))]
}
return string(b)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment