|
| 1 | +/** |
| 2 | + * UNDER CONSTRUCTION - API UNSTABLE |
| 3 | + */ |
| 4 | +// TODO: Consider alternative implementation of KeyObject subtype. |
| 5 | +module KeyObject = { |
| 6 | + type t('a, 'b); |
| 7 | + type symmetric = [ | `Symmetric]; |
| 8 | + type asymmetric = [ | `Asymmetric]; |
| 9 | + type publicKey = [ | `PublicKey]; |
| 10 | + type privateKey = [ | `PrivateKey]; |
| 11 | + type secretKey = [ | `SecretKey]; |
| 12 | + [@bs.send] |
| 13 | + external symmetricExport: |
| 14 | + t(symmetric, [< publicKey | privateKey]) => Buffer.t = |
| 15 | + "export"; |
| 16 | + [@bs.send] |
| 17 | + external asymmetricExport: |
| 18 | + t(asymmetric, [< publicKey | privateKey | secretKey]) => Buffer.t = |
| 19 | + "export"; |
| 20 | + module Symmetric = { |
| 21 | + type kind = [ symmetric]; |
| 22 | + type nonrec t('a) = t([ kind], 'a); |
| 23 | + module Impl = {}; |
| 24 | + }; |
| 25 | + module Asymmetric = { |
| 26 | + type kind = [ asymmetric]; |
| 27 | + type nonrec t('a) = t([ kind], 'a); |
| 28 | + module Impl = {}; |
| 29 | + }; |
| 30 | + module Impl = { |
| 31 | + include Symmetric.Impl; |
| 32 | + include Asymmetric.Impl; |
| 33 | + }; |
| 34 | + include Impl; |
| 35 | +}; |
| 36 | + |
| 37 | +module PivateKey = { |
| 38 | + include KeyObject.Impl; |
| 39 | + type kind = [ KeyObject.publicKey]; |
| 40 | + type t('a) = KeyObject.t('a, [ kind]); |
| 41 | + [@bs.module "crypto"] external make: Buffer.t => t('a) = "createPrivateKey"; |
| 42 | + [@bs.module "crypto"] |
| 43 | + external makeWithPassphrase: |
| 44 | + { |
| 45 | + .. |
| 46 | + "key": Buffer.t, |
| 47 | + "passphrase": Buffer.t, |
| 48 | + } => |
| 49 | + t('a) = |
| 50 | + "createPrivateKey"; |
| 51 | +}; |
| 52 | + |
| 53 | +module PublicKey = { |
| 54 | + include KeyObject.Impl; |
| 55 | + type kind = [ KeyObject.publicKey]; |
| 56 | + type t('a) = KeyObject.t('a, [ kind]); |
| 57 | + [@bs.module "crypto"] external make: Buffer.t => t('a) = "createPublicKey"; |
| 58 | + [@bs.module "crypto"] |
| 59 | + external fromPrivateKey: KeyObject.t('a, [> KeyObject.privateKey]) => t('a) = |
| 60 | + "createPublicKey"; |
| 61 | +}; |
| 62 | + |
| 63 | +module Hash = { |
| 64 | + type kind('w, 'r) = [ Stream__.transform('w, 'r) | `Hash]; |
| 65 | + type subtype('w, 'r, 'a) = Stream__.subtype([> kind('w, 'r)] as 'a); |
| 66 | + type supertype('w, 'r, 'a) = Stream__.subtype([< kind('w, 'r)] as 'a); |
| 67 | + type t = subtype(Buffer.t, Buffer.t, kind('w, 'r)); |
| 68 | + module Impl = { |
| 69 | + include Stream__.Transform.Impl; |
| 70 | + [@bs.send] external copy: t => t = "copy"; |
| 71 | + [@bs.send] external digest: t => Buffer.t = "digest"; |
| 72 | + [@bs.send] external update: (t, Buffer.t) => unit = "update"; |
| 73 | + }; |
| 74 | + include Impl; |
| 75 | +}; |
| 76 | + |
| 77 | +[@bs.module "crypto"] external createHash: string => Hash.t = "createHash"; |
| 78 | + |
| 79 | +module Hmac = { |
| 80 | + type kind('w, 'r) = [ Stream__.transform('w, 'r) | `Hmac]; |
| 81 | + type subtype('w, 'r, 'a) = Stream__.subtype([> kind('w, 'r)] as 'a); |
| 82 | + type supertype('w, 'r, 'a) = Stream__.subtype([< kind('w, 'r)] as 'a); |
| 83 | + type t = subtype(Buffer.t, Buffer.t, kind('w, 'r)); |
| 84 | + module Impl = { |
| 85 | + include Stream__.Transform.Impl; |
| 86 | + [@bs.send] external digest: t => Buffer.t = "digest"; |
| 87 | + [@bs.send] external update: (t, Buffer.t) => unit = "update"; |
| 88 | + }; |
| 89 | + include Impl; |
| 90 | +}; |
| 91 | + |
| 92 | +[@bs.module "crypto"] |
| 93 | +external createHmac: (string, ~key: string) => Hmac.t = "createHmac"; |
| 94 | + |
| 95 | +module Certificate = { |
| 96 | + type t; |
| 97 | + [@bs.send] |
| 98 | + external exportChallenge: (t, Buffer.t) => Buffer.t = "exportChallenge"; |
| 99 | + [@bs.send] |
| 100 | + external exportPublicKey: (t, Buffer.t) => Buffer.t = "exportPublicKey"; |
| 101 | + [@bs.send] |
| 102 | + external verifyCertificate: (t, Buffer.t) => bool = "verifyCertificate"; |
| 103 | +}; |
| 104 | + |
| 105 | +module Cipher = { |
| 106 | + type kind('w, 'r) = [ Stream__.transform('w, 'r) | `Cipher]; |
| 107 | + type subtype('w, 'r, 'a) = Stream__.subtype([> kind('w, 'r)] as 'a); |
| 108 | + type supertype('w, 'r, 'a) = Stream__.subtype([< kind('w, 'r)] as 'a); |
| 109 | + type t = subtype(Buffer.t, Buffer.t, kind('w, 'r)); |
| 110 | + module Impl = { |
| 111 | + include Stream__.Transform.Impl; |
| 112 | + [@bs.send] external final: (t, string) => Buffer.t = "final"; |
| 113 | + [@bs.send] external setAAD: (t, Buffer.t) => t = "setAAD"; |
| 114 | + [@bs.send] |
| 115 | + external setAADWith: |
| 116 | + ( |
| 117 | + t, |
| 118 | + Buffer.t, |
| 119 | + ~options: Stream__.Transform.makeOptions(Buffer.t, Buffer.t) |
| 120 | + ) => |
| 121 | + t = |
| 122 | + "setAAD"; |
| 123 | + [@bs.send] external getAuthTag: t => Buffer.t = "getAuthTag"; |
| 124 | + [@bs.send] external setAutoPadding: (t, bool) => t = "setAutoPadding"; |
| 125 | + [@bs.send] external update: (t, Buffer.t) => Buffer.t = "update"; |
| 126 | + }; |
| 127 | + include Impl; |
| 128 | + [@bs.module "crypto"] |
| 129 | + external make: |
| 130 | + ( |
| 131 | + ~algorithm: string, |
| 132 | + ~key: KeyObject.t('a, [> KeyObject.secretKey]), |
| 133 | + ~iv: Js.Null.t(Buffer.t) |
| 134 | + ) => |
| 135 | + t = |
| 136 | + "createCipheriv"; |
| 137 | + [@bs.module "crypto"] |
| 138 | + external makeWith: |
| 139 | + ( |
| 140 | + ~algorithm: string, |
| 141 | + ~key: KeyObject.t('a, [> KeyObject.secretKey]), |
| 142 | + ~iv: Js.Null.t(Buffer.t), |
| 143 | + ~options: Stream__.Transform.makeOptions(Buffer.t, Buffer.t)=? |
| 144 | + ) => |
| 145 | + t = |
| 146 | + "createCipheriv"; |
| 147 | +}; |
| 148 | + |
| 149 | +module Decipher = { |
| 150 | + type kind('w, 'r) = [ Stream__.transform('w, 'r) | `Decipher]; |
| 151 | + type subtype('w, 'r, 'a) = Stream__.subtype([> kind('w, 'r)] as 'a); |
| 152 | + type supertype('w, 'r, 'a) = Stream__.subtype([< kind('w, 'r)] as 'a); |
| 153 | + type t = subtype(Buffer.t, Buffer.t, kind('w, 'r)); |
| 154 | + module Impl = { |
| 155 | + [@bs.send] |
| 156 | + external final: (subtype('w, 'r, 'a), string) => Buffer.t = "final"; |
| 157 | + [@bs.send] |
| 158 | + external setAAD: (subtype('w, 'r, 'a), Buffer.t) => t = "setAAD"; |
| 159 | + [@bs.send] |
| 160 | + external setAADWith: |
| 161 | + ( |
| 162 | + subtype('w, 'r, 'a), |
| 163 | + Buffer.t, |
| 164 | + ~options: Stream__.Transform.makeOptions(Buffer.t, Buffer.t) |
| 165 | + ) => |
| 166 | + t = |
| 167 | + "setAAD"; |
| 168 | + [@bs.send] |
| 169 | + external setAuthTag: (subtype('w, 'r, 'a), Buffer.t) => t = "setAuthTag"; |
| 170 | + [@bs.send] |
| 171 | + external setAutoPatting: (subtype('w, 'r, 'a), bool) => t = |
| 172 | + "setAutoPadding"; |
| 173 | + [@bs.send] |
| 174 | + external update: (subtype('w, 'r, 'a), Buffer.t) => Buffer.t = "update"; |
| 175 | + }; |
| 176 | + include Impl; |
| 177 | + [@bs.module "crypto"] |
| 178 | + external make: |
| 179 | + ( |
| 180 | + ~algorithm: string, |
| 181 | + ~key: KeyObject.t('a, [> KeyObject.secretKey]), |
| 182 | + ~iv: Js.Null.t(Buffer.t) |
| 183 | + ) => |
| 184 | + t = |
| 185 | + "createDecipheriv"; |
| 186 | + [@bs.module "crypto"] |
| 187 | + external makeWith: |
| 188 | + ( |
| 189 | + ~algorithm: string, |
| 190 | + ~key: KeyObject.t('a, [> KeyObject.secretKey]), |
| 191 | + ~iv: Js.Null.t(Buffer.t), |
| 192 | + ~options: Stream__.Transform.makeOptions(Buffer.t, Buffer.t)=? |
| 193 | + ) => |
| 194 | + t = |
| 195 | + "createDecipheriv"; |
| 196 | +}; |
| 197 | + |
| 198 | +// module DiffieHellman = { |
| 199 | + |
| 200 | +// }; |
| 201 | + |
| 202 | +// module DiffieHellmanGroup = { |
| 203 | + |
| 204 | +// }; |
| 205 | + |
| 206 | +// module Sign = { |
| 207 | + |
| 208 | +// }; |
| 209 | + |
| 210 | +// module Verify = { |
| 211 | + |
| 212 | +// }; |
0 commit comments