Fork me on GitHub
#babashka
<
2023-11-24
>
Eugen05:11:59

hi @borkdude I noticed the Arrays/fill method is missing - while implementing pkcs7 padding for ansible vault (code block in thread). I can probably work around it with Arrays/copyRange + some custom code but was curios as to why it is not there: intention or ??

; java.lang.IllegalArgumentException: No matching method fill found taking 4 args user /home/ieugen/proiecte/clojure/compoje/src/compoje/providers/ansible_vault.clj:3:22
https://github.com/babashka/babashka/blob/2a432d3e802c695391da909601044824183ad053/src/babashka/impl/classes.clj#L91

Eugen05:11:43

(defn pad-pkcs7
  "Inspired from docs:
   
   PKCS7 padding is a generalization of PKCS5 padding (also known as standard padding).
   PKCS7 padding works by appending N bytes with the value of chr(N),
   where N is the number of bytes required to make the final block of
   data the same size as the block size."
  [block-size-bits buffer]
  (let [block-size-bytes (block-size-bits->bytes block-size-bits)
        buffer-size-bytes (int (count buffer))
        buffer-with-padding-size (padded-buffer-size block-size-bytes buffer-size-bytes)
        n (padding-value block-size-bytes buffer-size-bytes)
        padding-required? (pos-int? n)]
    (if padding-required?
      (let [bytes (byte-array buffer-with-padding-size buffer)
            to-index (int (+ buffer-size-bytes n))]
        (Arrays/fill bytes buffer-size-bytes to-index (byte n))
        bytes)
      buffer)))

borkdude10:11:32

Added the fill method for next release

Eugen10:11:49

thank you

borkdude10:11:15

The reason that not everything is in there is that the Array class pulls in a lot of other stuff which increases the size significantly

Eugen10:11:23

I implemented padding- which was the missing part and now I will focus on the API and features

Eugen10:11:29

thank you, did not know that

borkdude10:11:26

$ ./bb -e '(let [arr (int-array [1 2 3])] (java.util.Arrays/fill arr 2) (into [] arr))'
[2 2 2]
Seems to work now

Eugen10:11:11

great. I could do it via loop most likely - but I suspect fill performance might be much better than sci

borkdude10:11:32

You can also use areduce