|
1 | 1 | package pqsignature
|
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "image/png" |
| 8 | + "os" |
| 9 | + "path/filepath" |
| 10 | + |
| 11 | + "github.com/fogleman/gg" |
| 12 | + "github.com/makiuchi-d/gozxing" |
| 13 | + "github.com/makiuchi-d/gozxing/qrcode" |
| 14 | +) |
| 15 | + |
| 16 | +// Encode splits input msg into chunks to fit max supported length of QR code message and generates an array of QR codes images. |
| 17 | +func Encode(msg string, alias string, outputDir string, outputFileTitle string, outputFileNamePattern string, outputFileNameSuffix string) ([]Image, error) { |
| 18 | + if _, err := os.Stat(outputDir); os.IsNotExist(err) { |
| 19 | + if err := os.MkdirAll(outputDir, 0770); err != nil { |
| 20 | + return nil, errors.New(err) |
| 21 | + } |
| 22 | + } |
| 23 | + pngs, err := toPngs(msg, MaxMsgLength, DataQRImageSize) |
| 24 | + if err != nil { |
| 25 | + return nil, err |
| 26 | + } |
| 27 | + var images []Image |
| 28 | + for i, imageBytes := range pngs { |
| 29 | + filePathPartNumber := "" |
| 30 | + titlePartNumber := "" |
| 31 | + if len(pngs) > 1 { |
| 32 | + filePathPartNumber = fmt.Sprintf("__part_%v_of_%v", i+1, len(pngs)) |
| 33 | + titlePartNumber = fmt.Sprintf(" part %v of %v", i+1, len(pngs)) |
| 34 | + } |
| 35 | + partTitle := fmt.Sprintf("%v%v", outputFileTitle, titlePartNumber) |
| 36 | + filePath := filepath.Join(outputDir, fmt.Sprintf("%v%v%v.png", outputFileNamePattern, filePathPartNumber, outputFileNameSuffix)) |
| 37 | + err := os.WriteFile(filePath, imageBytes, 0644) |
| 38 | + if err != nil { |
| 39 | + return nil, errors.New(err) |
| 40 | + } |
| 41 | + img, err := gg.LoadImage(filePath) |
| 42 | + if err != nil { |
| 43 | + return nil, errors.New(err) |
| 44 | + } |
| 45 | + images = append(images, Image{ |
| 46 | + raw: imageBytes, |
| 47 | + title: partTitle, |
| 48 | + image: img, |
| 49 | + alias: alias, |
| 50 | + }) |
| 51 | + } |
| 52 | + return images, nil |
| 53 | +} |
| 54 | + |
| 55 | +func toPngs(s string, dataSize int, qrImageSize int) ([][]byte, error) { |
| 56 | + chunks := breakStringIntoChunks(s, dataSize) |
| 57 | + var qrs [][]byte |
| 58 | + for _, chunk := range chunks { |
| 59 | + enc := qrcode.NewQRCodeWriter() |
| 60 | + image, err := enc.Encode(chunk, gozxing.BarcodeFormat_QR_CODE, qrImageSize, qrImageSize, nil) |
| 61 | + if err != nil { |
| 62 | + return nil, errors.New(err) |
| 63 | + } |
| 64 | + pngBuffer := new(bytes.Buffer) |
| 65 | + err = png.Encode(pngBuffer, image) |
| 66 | + if err != nil { |
| 67 | + return nil, errors.New(err) |
| 68 | + } |
| 69 | + qrs = append(qrs, pngBuffer.Bytes()) |
| 70 | + } |
| 71 | + return qrs, nil |
| 72 | +} |
| 73 | + |
| 74 | +func breakStringIntoChunks(str string, size int) []string { |
| 75 | + if len(str) <= size { |
| 76 | + return []string{str} |
| 77 | + } |
| 78 | + var result []string |
| 79 | + chunk := make([]rune, size) |
| 80 | + pos := 0 |
| 81 | + for _, rune := range str { |
| 82 | + chunk[pos] = rune |
| 83 | + pos++ |
| 84 | + if pos == size { |
| 85 | + pos = 0 |
| 86 | + result = append(result, string(chunk)) |
| 87 | + } |
| 88 | + } |
| 89 | + if pos > 0 { |
| 90 | + result = append(result, string(chunk[:pos])) |
| 91 | + } |
| 92 | + return result |
| 93 | +} |
0 commit comments